StudioPress: add meta tag to every page [closed]

This function should take care for the input into the head of each page/post as asked. Please make a copy of functions.php before adding following code.Adjust to your own preferences if needed. /** * Add meta to head * * Read more {@link https://codex.wordpress.org/Plugin_API/Action_Reference/wp_head} * @version WordPress 4.8 */ function wpse272951_wsm_keep_ie_modern() { echo “<meta http-equiv=\”X-UA-Compatible\” … Read more

Looping through WP_Post Object

Based on what you said about a post being completed, you can check if its status is completed and if it is, then point to the title: foreach ( $lessons as $lesson ){ if( $lesson[‘status’] == ‘completed’ ){ echo $lesson[‘post’]->post_title; } }

Passing data between two hooks in separate HTML requests

It’s not possible to pass data from one HTTP request to another one on the fly. Once the script is finished, the data will be discarded. What you can do is to store the data in a transient, and then retrieve it later. Here’s a simple example using set_transient(): set_transient( ‘my_transient’, $data, 1 * HOUR_IN_SECONDS … Read more

register_rest_route regex option for base64 or alternate

I got curious, so I tested this barebone demo: add_action( ‘rest_api_init’, function () { register_rest_route( ‘wpse/v1’, ‘/post_by_permalink/(?P<path>[\S]+)’, [ ‘methods’ => WP_REST_Server::READABLE, ‘callback’ => ‘wpse_callback’, ‘show_in_rest’ => true ] ); }); function wpse_callback( $request ) { $data = [ ‘path’ => base64_decode( $request[‘path’] ) ]; return $data; } Testing: https://example.com/wp-json/wpse/v1/post_by_permalink/aHR0cHM6Ly93b3JkcHJlc3Muc3RhY2tleGNoYW5nZS5jb20vcS8zMDEwNjcv gives { path: “https://wordpress.stackexchange.com/q/301067/” } Tested … Read more

WPDB: how to get the value of a field in a custom database table

OK, so first of all, you should always use proper escaping, when building the queries. This is how your code can look after fixing: global $wpdb; $value = $wpdb->get_var( $wpdb->prepare( ” SELECT column_name FROM {$wpdb->prefix}plugin_table WHERE ID = %d “, get_current_user_id() ) ); The things you have to take care of: You have to change … Read more

Loading page template into shortcode

get_template_part takes slug as first parameter and not filename. So it should be: get_template_part( ‘template-sponsors’ ); And with more details… This function takes two parameters: get_template_part( string $slug, string $name = null ) And inside of it, the name of a file is built like this: if ( ” !== $name )         $templates[] = “{$slug}-{$name}.php”;       $templates[] = “{$slug}.php”; So, … Read more

is_page(id) not working for blog page

If the page you’re trying to check is set as Page for posts, then is_page conditional won’t be true. In such case you should check if is_home. Why? All these conditional tags are based on global wp_query. For page that is set as page for posts (home of your blog), the global query doesn’t contain … Read more

Find variables available at a given hook

In general the best solution would be to refer to the documentation for the hook – or the source for the action call within WordPress core’s codebase if the documentation proves insufficient. You can search the documentation for the relevant action or filter here. Many pages actually include the source of the filter/action call which … Read more

How do I hardcode a WordPress shortcode into my theme?

Check out do_shortcode(): http://codex.wordpress.org/Function_Reference/do_shortcode do_shortcode(‘[shortcode option1=”value1″ option2=”value2″]’); So your example would be: do_shortcode(‘[post_comments]’); What might be easier is to tap into the underlying comment functions: http://codex.wordpress.org/Function_Reference/comments_number <p> This post currently has <?php comments_number( ‘no responses’, ‘one response’, ‘% responses’ ); ?>. </p> You can also use get_comments_number which returns the value rather than printing it … Read more