How to preserve edits to Name or Slug of term when using wp_update_term on save?

So, thanks to cjbj I finally found the correct answer! I needed to use edited_term instead of edit_term. Very subtle difference. edited_term fires after a term has been saved. // insert stuff into description field of taxonomy function insert_taxonomy_content( $term_id, $tt_id, $taxonomy ){ // only insert content on certain taxonomies if ( $taxonomy === ‘some_custom_taxonomy’ … Read more

Add class or ID to any WordPress function

If you have only one template it’s fine to do something like: echo ‘<p class=”whatever”>’ . get_the_excerpt() . ‘</p>’; However, if you have multiple templates and want to control the classes centrally, you can make a filter on get_the_excerpt as follows (but yeah, that would be in functions.php): add_filter (‘get_the_excerpt’,’wpse240352_filter_excerpt’); function wpse240352_filter_excerpt ($post_excerpt) { $post_excerpt=”<p … Read more

Functions – callback vs fallback

That is not really a question in the focus of WordPress, is also in other programming languages and in our speaking language a topic. Maybe I can clear the difference between these words with a non coding example. Callback A return of a situation to a state. Like a call with the telephone to a … Read more

Count total number of images in post and echo results as number

Use regex to find all the urls and filter by type. $post = get_post( 504 ); $content = $post->post_content; // match all urls preg_match_all( ‘/(http:|https:)?\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/’, $content, $matches ); $count = 0; if ( ! empty( $matches ) && ! empty( $matches[ 0 ] ) ) { foreach ( $matches[ 0 ] as $url ) { … Read more

Debugging with functions.php

You can use var_dump() instead of print_r() – you get the type and the value of the variable and it will also work when your variable holds FALSE or NULL. print_r( false ); # doesn’t output anything var_dump( false ); # output: bool(false) print_r( NULL ); # doesn’t output anything var_dump( NULL ); # output: … Read more

Pass parameters to function through an action

add_action() and add_filter() take 4 params: string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1. So, the function you are hooking to takes more than 1 param, then just pass the number of params it accepts as the 4th param to add_action(). In your case, add_action (‘my_action’, ‘my_function’, 10, 2) ;

Is It wrong to use oop approach on functions.php?

As described in the Child Themes entry in the codex: TIP FOR THEME DEVELOPERS. The fact that a child theme’s functions.php is loaded first means that you can make the user functions of your theme pluggable —that is, replaceable by a child theme— by declaring them conditionally. if ( ! function_exists( ‘theme_special_nav’ ) ) { … Read more

How to insert a text in all pages and posts before or after specific places?

You can add to before or after the content as well in a similar fashion. I use this code: function rt_before_after($content) { $beforecontent=”This goes before the content.”; $aftercontent=”And this will come after.”; $fullcontent = $beforecontent . $content . $aftercontent; return $fullcontent; } add_filter(‘the_content’, ‘rt_before_after’); You could use it similarly with the_title,. The reko_hook() you have … Read more