Automatically set the featured image

Method 1: Using the publish_post hook* and media_sideload_image() Here we will upload an image from a URL, attach it to the post, and set it as the featured image when the post is published. *Actually, we’re using the dynamic hook {$new_status}_{$post->post_type}, which refers transitioning a post post type to the publish status. See wp-includes/post.php for … Read more

Removing custom background and header feature in child theme

Both the header and background image features setup some globals in order to work, unsetting those globals seems to have some effect, and at the least removes them from the administration side. add_action(‘after_setup_theme’, ‘remove_theme_features’, 11 ); function remove_theme_features() { $GLOBALS[‘custom_background’] = ‘kill_theme_features’; $GLOBALS[‘custom_image_header’] = ‘kill_theme_features’; } class kill_theme_features { function init() { return false; } … Read more

Variable scope in WordPress functions.php

What is the scope of variables you declare at the top of functions.php, outside of any of the individual functions? This is a general PHP question… Variables inside a function are only available inside that function. Variables outside of functions are available anywhere outside of functions, but not inside any function. This means there’s one … Read more

Enqueue Script with data attributes

Please try code given below: function add_data_attribute($tag, $handle) { if ( ‘chargebee’ !== $handle ) return $tag; return str_replace( ‘ src’, ‘ data-cb-site=”mydomainame” src’, $tag ); } add_filter(‘script_loader_tag’, ‘add_data_attribute’, 10, 2);

why doesnt is_home() work in functions.php

At the time functions.php is included during bootup, WordPress has no idea on the contents of the query, and doesn’t know the nature of the page. is_home will return false. Wrap the code in a function and have it triggered by the wp hook, which comes after the global query object has been hydrated with … Read more

bloginfo() vs get_option?

The two functions output exactly the same thing. From the Codex entry for get_bloginfo(): ‘name’ – Returns the “Site Title” set in Settings > General. This data is retrieved from the “blogname” record in the wp_options table. From source: case ‘name’: default: $output = get_option(‘blogname’); Neither get_bloginfo() nor bloginfo() do any sort of sanitization or … Read more

How to use get_template_directory_uri() to load an image that is in a sub-folder of my theme?

You should echo it and also you are closing your php tag improperly. View source the o/p generated to get some idea img<src=<?php echo get_template_directory_uri().”/assets/img/flexslider/flex-1.jpg”?> alt=”alternative_name”> or you can use bloginfo which is easier to remember and use (You need not echo) <img src=”<?php bloginfo(‘template_url’); ?>/assets/img/flexslider/flex-1.jpg”/>