Call add_action() in function wordpress

You’re thinking about this entirely wrong. A meta tag isn’t something you add when a post gets saved, it’s something that gets added to the output when a post is viewed. So instead of trying to hook the action inside save_post, you hook it on every page load, and inside the hook you check if … Read more

Search function works improperly

This code should work OK, but… There are some problems with it. Let’s break it apart and add some comments. function searchcategory($query) { if ($query->is_search) { // Checking only for is_search is very risky. It will be set to true // whenever param “s” is set. So your function will modify any wp_query with s, … Read more

How to change the order (priority) of registered filters (or actions) (e.g. for the_content)?

If you know the existing callback and priority you can just remove the filters and then add again at a different priority: remove_filter( ‘the_content’, ‘convert_smilies’, 20 ); add_filter( ‘the_content’, ‘convert_smilies’, 30 ); remove_filter( ‘the_content’, ‘capital_P_dangit’, 11 ); add_filter( ‘the_content’, ‘capital_P_dangit’, 20 );

Delay an action until current action is completed

Note that the third input argument for the save_post callback, tells us whether it’s an update or not. That could be helpful here, I think. Taking your example as is, one suggestion could be: add_action( ‘save_post’, function( $post_ID, $post, $update ) { postUpdate( $post_ID ); if ( $update ) { deleteCacheOnCloudfront( $post_ID, $post, $update ); … Read more

how can I use add_action run another function when publishing new post?

You’re partly there. This is the code you should be using: add_action( ‘publish_post’, array( $myClass ,’dothisfunction’ ), 10, 2 ); You can’t pass parameters to the action that’s being executed. You can only “hook” into the action after do_action is called, and whatever parameters have been added to that action. If there are particular variables … Read more

Execute Hook on the footer or header after activating a plugin

Checking the action hooks API reference, “activated_plugin” is an Advanced hook. It operates outside the normal loop which would permit you to do what you want. A better way would be to add_action(‘admin_footer_text’), as illustrated in this WPBeginner article. However, if you only want this to show when a particular plugin is activated, you can … Read more

How to manage arrays from custom functions stored in functions.php?

A callback for an action doesn’t return anything, because that return value is never passed through by WordPress. Use a filter instead: add_filter( ‘listofnames’, ‘SomeNames’ ); function SomeNames() { $names=array( “john”,”edgar”,”miles”); return $names; } And in your template you call it like this: $names = apply_filters( ‘listofnames’, [] ); foreach ( $names a $name ) … Read more