Shortcode with custom content attribute?

The second parameter of your shortcode function is the content. So rewrite the function: function shortcode_call_to_action( $attributes = NULL, $content=”” ) { $stylesheetdir = get_bloginfo(‘stylesheet_directory’); if ( ” === $content ) { // use a default text if there is no content $content=”default text”; } return “<div id=’call_to_action’> <img src=”https://wordpress.stackexchange.com/questions/58438/$stylesheetdir/images/call_to_action.jpg” alt=”” /> <p>$content</p>”; } If … Read more

Dynamically Register Sidebars For Each Top Level Page

try this code for creating unique sidebar for each parent page you can use $page->post_name(page slug) or $page->ID(page id) as sidebar id if ( function_exists(‘register_sidebar’) ){ /*extract all parent pages */ $topLevel = get_pages(array( ‘sort_column’ => ‘post_date’, ‘hierarchical’ => 0, ‘parent’ => 0 )); foreach($topLevel as $page){ /* register sidebar for each parent page */ … Read more

Add inline css to theme

According to Codex, this function accepts 2 arguments: <?php wp_add_inline_style( $handle, $data ); ?> Take a look at this example: function my_inline_css() { wp_enqueue_style( ‘custom-style’, get_template_directory_uri() . ‘/css/custom_script.css’ ); $btn_color = esc_attr( get_theme_mod( ‘my-custom-color’ ) ); $custom_css = ” .btn-default{ background-color: {$btn_color}; }”; wp_add_inline_style( ‘custom-style’, $my_custom_css ); } add_action( ‘wp_enqueue_scripts’, ‘my_inline_css’ ); You should have … Read more

Child theme – copied some files from parent to child website still uses parent files

Try to override the parent theme functions instead of creating your child theme header-functions.php file. Here is a reference on how it can be done : https://www.mhthemes.com/support/overriding-parent-theme-functions-child-theme-tutorial/ Check if the parent theme header-functions.php file is implementing pluggable functions. Then you can override them in your functions.php file.

write in functions.php

I’m not sure why you don’t just insert the code in the single.php, or use Denis’s solution, but if you want to hook into the_content you can do so by putting the following in your functions.php file: function append_the_content($content) { $content .= ‘PUT YOUR FUNCTION HERE’; return $content; } add_filter(‘the_content’, ‘append_the_content’); This will add directly … Read more

How often is functions.php loaded?

Simple answer is once. As any other PHP file otherwise it would likely cause crash because of function redefinitions. More complex answer is there can be actually two different functions.php loading – one for parent theme and one for child theme. Tricky part here is that child theme’s loads before the parent’s, which can at … Read more

Display the number of user comments

Got some idea from here. User’s comments have posted <?php function commentCount() { global $wpdb; $count = $wpdb->get_var(‘SELECT COUNT(comment_ID) FROM ‘ . $wpdb-comments. ‘ WHERE comment_author_email = “‘ . get_comment_author_email() . ‘”‘); echo $count . ‘ comments’; } commentCount(); ?> I don’t have any idea for comments that user got from their posts. Might be … Read more

Reuse variable in hook callback

If you want to use a variable from a another context in a function, create a class: class CustomLog { private $debug_msg = “”; public function __construct( $msg ) { $this->debug_msg = $msg; } public function log() { error_log( $this->debug_msg, 0 ); } } add_action( ‘init’, [ new CustomLog( “Hi there!” ), ‘log’ ], 20 … Read more