Skip to content

Instantly share code, notes, and snippets.

@troutcolor
Last active August 10, 2020 16:27
Show Gist options
  • Save troutcolor/d8efe232e030fcb94660fcb4813a98f0 to your computer and use it in GitHub Desktop.
Save troutcolor/d8efe232e030fcb94660fcb4813a98f0 to your computer and use it in GitHub Desktop.
functions that have do with micro.blog and microblogging that live in my child theme's functions.php
<?php // Opening PHP tag - nothing should be before this, not even whitespace
//Micro.blog//
/*
updated 2018-01-29
this is just the micro.blog stuff I've added to my child theme's function php
Hopefully most of the credits are noted below
doing the following
1. Adding titles when editing status posts in editor with updt_set_post_title
2. Adding titles to post that arrive from anywhere, micro.blog app, applescript etc with modify_post_title
3. get posts with :wwwd: in the body, change cat t owwwd and fromat to standard (no format)
4. not marking any webmentions as spam with unspam_webmentions by pre approving webmentions
5. Getting posts with micro category off my home page with exclude_status_home
6. Removing titles from posts categorised as Micro in the RSS feed with jjmicroblog_titlelessrss
7. Add microformat 2 markup to featured images
8. Remove Fake Emoji -- since I am using emoji as a taxonomy
9. get emoji out of slugs -- since I am using emoji as a taxonomy
*/
/*
1. Adds title in dashboard not xml-rpc, probably don't want this although it might help if I post through dashboard
Should probably do the if else the other way, so if there is a title it gets kept...
this was useful
https://github.com/jonathanwhitelandnet/wp-micro-blog/blob/master/wp-micro-blog.php
*/
function updt_set_post_title( $title )
{
global $post, $_POST;
$type = get_post_format( $post->ID );
if ( strlen($title) > 2) {return $title;}
if ( "status" == $type ) {
$title = substr( wp_filter_nohtml_kses( $_POST[ 'content' ] ) , 0 , 60 );
return $title ;
} else {
return $title;
}
}
add_filter('title_save_pre', 'updt_set_post_title');
/*
2. hopefully adds titles to xml-rpc posts coming from micro.blog app
https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
I wish I knew what the '99',1 bit was for...
*/
add_action('wp_insert_post_data', 'modify_post_title', '99', 1); // Grabs the inserted post data so you can modify it.
//, $postarr
function modify_post_title($data, $postarr)
{
if ($data[ 'post_title' ] == '' && $data[ 'post_type' ] == 'post' ) {
//wp_filter_nohtml_kses strips html and then I replace &nbsp;
$title = str_replace( "&nbsp;" , " " , substr( wp_filter_nohtml_kses( $data[ 'post_content' ] ) , 0, 60 ) ) . "..." ;
$data['post_title'] = $title ;
}
//also allow comments on all posts including ones coming from microblog app
//should probably change the name of this function
//if( $data['post_type'] == 'post' ) {
// $data['comment_status'] = 'open';
//}
return $data; // Returns the modified data.
}
//colin walker improve turn on comments on posts coming from xml-rpc
add_action ('xmlrpc_call', 'check_new_post' );
function check_new_post( $method ) {
if( 'wp.newPost' === $method ) {
add_filter( 'wp_insert_post_data', 'open_comments', 100, 1 );
}
}
function open_comments ($postarr) {
$postarr[comment_status] = 'open';
return $postarr;
}
/*
3. change formats and categories
any post coming from micro.blog app with :wwwd: in it gets the fromat changed to "" and the category wwwd added
need to do this in two stages:
detect the :wwws: string on wp_insert_post_data
stash thart in a global
and change format and set category
*/
add_filter( 'wp_insert_post_data' , 'jj_detect_format' );
function jj_detect_format( $data ) {
global $jj_format;
// detect format in $data[ 'post_content' ] and modify it
if (stristr( $data['post_content'] , ":wwwd:" )){
$jj_format = "wwwd";
$data['post_content'] =str_replace(":wwwd:","",$data['post_content']);
}
//
return $data;
}
add_action('wp_insert_post' , 'jj_sort_format');
function jj_sort_format( $post_ID ) {
global $jj_format;
if ($jj_format=="wwwd"){
//setting format to standard
set_post_format($post_ID, "");
$categories=array(6);
wp_set_post_categories( $post_ID, $categories, true );
}
}
//end change formats and categories
/*
4. pre approve all webmentions so not spam
* as suggested by snarfed at https://github.com/indieweb/wordpress-indieweb/issues/38
* https://indieweb.org/WordPress_with_Bridgy#Spam
*/
function unspam_webmentions($approved, $commentdata)
{
return $commentdata[ 'comment_type' ] == 'webmention' ? 1 : $approved;
}
add_filter('pre_comment_approved', 'unspam_webmentions', '99', 2);
/*
5. keep status format off home page
updated with code from @ciudadanob http://micro.blog/ciudadanob posted in the micro.blog slack
Was warned by Tom Nowell that NOT IN was expensive
*/
function exclude_status_home($query)
{
if ($query->is_home && $query->is_main_query()) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-status',
'operator' => 'NOT IN'
)
)
);
$query->set('tax_query', $args);
}
return $query;
}
add_filter('pre_get_posts', 'exclude_status_home');
/*
6. remove titles from Status posts in RSS
some help from
http://www.wpbeginner.com/wp-tutorials/how-to-add-content-and-completely-manipulate-your-wordpress-rss-feeds/
but changed to get_post_format
2018-08-11 don't remove titles if post > 280 chars
19 Nov 2018 replace emoji with . before counting chars
*/
function jjmicroblog_titlelessrss( $content )
{
$postformat = get_post_format();
$postcontent = get_the_content();
if ( 'status' == $postformat ) {
if (strlen(strip_tags(str_replace('&#xfe0f;','.',str_replace('&#x1f5d3;','.',str_replace('&#x1f4f7;','.',$postcontent)))))<280){
$content = '';
}
}
return $content ;
}
add_filter('the_title_rss', 'jjmicroblog_titlelessrss');
/*
This one is based on the one below that strips emoji
just replaces with a .
should just be one function with a param
*/
function replace_emoji_one_char($text){
return preg_replace('/([0-9#][\x{20E3}])|[\x{00ae}\x{00a9}\x{203C}\x{2047}\x{2048}\x{2049}\x{3030}\x{303D}\x{2139}\x{2122}\x{3297}\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u', '.', $text);
}
/*
7. Add microformat 2 markup to featured images see:
https://miklb.com/microformats2-wordpress-and-featured-images-classes/
*/
function mf2_featured_image($attr)
{
remove_filter('wp_get_attachment_image_attributes','mf2_featured_image');
$attr['class'] .= ' u-featured';
return $attr;
}
add_filter('wp_get_attachment_image_attributes','mf2_featured_image');
/*
8. Remove Fake Emoji
//https://mdhughes.tech/2018/01/11/fake-emoji-smart-quotes-fuck-off/
*/
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
add_filter('emoji_svg_url', '__return_false');
/*
9. get emoji out of slugs
https://stackoverflow.com/questions/47211216/wordpress-hook-on-slug-generator
*/
function remove_emoji($text){
return preg_replace( '/([0-9#][\x{20E3}])|[\x{00ae}\x{00a9}\x{203C}\x{2047}\x{2048}\x{2049}\x{3030}\x{303D}\x{2139}\x{2122}\x{3297}\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u', '', $text );
}
add_filter( 'wp_unique_post_slug', 'jjremove_emoji_wp_unique_post_slug', 2, 6 );
function jjremove_emoji_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug )
{
if ( $post_type == 'post')
{
$slug = remove_emoji( $slug );
}
return $slug;
}
/*
the theme supports all of the standard formats, I just want status and standard which has no format
I am moving to use kinds
*/
add_action( 'after_setup_theme', 'childtheme_formats', 11 );
function childtheme_formats(){
add_theme_support( 'post-formats', array( 'status') );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment