How to use a custom hook

The problem is the order of execution. The order things run matters. For your code to function, it would require either add_action to implement time travel several microseconds into the past so that it can add the hook before it ran, or it would require do_action to implement precognition so that it knows about add_action … Read more

Display related posts without a plugin

I slightly modified your code and tested on my WordPress installation. If you using a filter, like ‘the_content’, which recieves a content as an argument, you must also return this content back. Here is working version of your function with comments. //this filter recieves $content, which you should always return, //otherwise you will not see … Read more

Need to put a script above tag in header.php – WP 5.7.1

As @Buttered_Toest mentioned, this snippet works well. (This code goes to functions.php) function futy_widget_footer(){ ?> <script> window.Futy = { key: ‘0000000000’ }; (function (e, t) { var n = e.createElement(t); n.async = true; n.src=”https://v1.widget.futy.io/js/futy-widget.js”; var r = e.getElementsByTagName(t)[0]; r.parentNode.insertBefore(n, r); })(document, ‘script’); </script> <?php } add_action(‘wp_footer’,’futy_widget_footer’);

WordPress template page name displayed on screen

If your template file doesn’t have the <?php tag at the top, you should add it. <?php /* Template Name: Exhibition – Pilgrims */ …is how the very top of your template file should look. Your sample should read like this. <?php /* Template Name: Exhibition – Pilgrims */ get_header(); ?> <div class=”container fadeIn perimeter”> … Read more

admin-ajax Firing Error 400 When Logged In

Did I set up that wp_ajax_nopriv_ hook wrong? No, you did not. But that hook is for logged-out or unregistered users only. For logged-in users, the hook is wp_ajax_<action>. So you just need to change the add_action(‘get_events’ in your code to add_action(‘wp_ajax_get_events’, and the error 400 would be gone: add_action(‘wp_ajax_get_events’, ‘get_events_func’); // for logged-in users … Read more

Hide a div when tags doesnt exist [duplicate]

You need to first get and check if there are any tags available, so something like this <?php if (get_tags()) : ?> <div class=”etichete”> <p><?php the_tags(); ?></p> </div> <?php endif; ?> You can also use !empty(get_tags()) but because if no tags are available it will return an empty array and an empty array is considered … Read more

echo something inside a shortcode

There are a few to do this: Using double quotes Don’t forget to escape the double quotes inside the string. global $post; echo do_shortcode(“[sc name=\”$post->post_name\”]”); Using sigle quotes global $post; echo do_shortcode(“[sc name=”$post->post_name”]”); Using sprintf global $post; echo do_shortcode( sprintf(‘[sc name=”%s”]’, $post->post_name) ); Using wp_sprintf global $post; echo do_shortcode( wp_sprintf(‘[sc name=”%s”]’, $post->post_name) ); Using heredoc … Read more

Illegal string offset in PHP function

You need to be validating a couple of parts about $filearray before you use it. There are some good suggestions in the comments, I think a couple of robust examples would serve you well too: Using empty() function wp_custom_attachment() { wp_nonce_field( plugin_basename(__FILE__), ‘wp_custom_attachment_nonce’ ); $html=”<p class=”description”>Upload your PDF here.</p>”; $html .= ‘<input id=”wp_custom_attachment” name=”wp_custom_attachment” size=”25″ … Read more

Show meta box only for default page template

If the “Default Template” option is selected from the Template dropdown, then WordPress will set the value of the _wp_page_template meta to default and not page.php, so that’s most likely the reason why your meta box is not showing/added. So I would use if ( in_array( $pageTemplate, array( ‘default’, ‘page.php’ ) ) ) instead of … Read more