Best practice custom function, where to echo the variables?

Generically speaking, yes. If your normal output would be to echo at the end, echoing during the loop wouldn’t change much. function get_some_posts($args) { $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); the_title(); // the_title() is the same as echo get_the_title(); } } } add_action(‘custom_hook’,’get_some_posts’); The … Read more

Gutenberg gallery hook errors

Seems that when you modify the existing block’s HTML, then it is no longer being validated. Instead, you might try to create your own block. Look here is a good approach and here too.

Display $pagenow error notice on all admin pages

function my_admin_notice() { /* * The class of admin notice should be “notice” plus any one of * -“notice-error”, * -“notice-warning”, * -“notice-success” * -“notice-info”. * Optionally use “is-dismissible” to apply a closing icon. */ echo ‘<div class=”notice notice-info”><p>Custom notice text</p></div>’; } add_action( ‘admin_notices’, ‘my_admin_notice’ ); The only thing is, these notices does not show … Read more

WordPress tag cloud add more links

Yes, you can. WP_Widget_Tag_Cloud widget uses wp_tag_cloud to generate the tag cloud. And inside that function and at the end of that function you can find this: /** * Filters the tag cloud output. * * @since 2.3.0 * * @param string $return HTML output of the tag cloud. * @param array $args An array … Read more

merging an array to an existing array using add_filter

You are overwriting $args in add_yourself_to_some_examplehook(). Based on what you’re saying you want to achieve, you should be appending the new item instead: add_filter( ‘some_example_hook’, ‘add_yourself_to_some_examplehook’ ); function add_yourself_to_some_examplehook($args) { $args[ ‘Hello5’ ] = ‘HELL4’; // <– Note the syntax here. return $args; }

Override wp_delete_post and move to trash instead for custom post type

Inside wp_delete_post() there is the following code, /** * Filters whether a post deletion should take place. * * @since 4.4.0 * * @param bool|null $delete Whether to go forward with deletion. * @param WP_Post $post Post object. * @param bool $force_delete Whether to bypass the trash. */ $check = apply_filters( ‘pre_delete_post’, null, $post, $force_delete … Read more

How to prevent action in ajax request, when in admin side?

After testing a lot of things, I found this function, which works: function is_admin_request() { /** * Get current URL. * * @link https://wordpress.stackexchange.com/a/126534 */ $current_url = home_url( add_query_arg( null, null ) ); /** * Get admin URL and referrer. * * @link https://core.trac.wordpress.org/browser/tags/4.8/src/wp-includes/pluggable.php#L1076 */ $admin_url = strtolower( admin_url() ); $referrer = strtolower( wp_get_referer() ); … Read more