the function dynamic_sidebar() not working at all - php

im developping a wordpress theme and i nedded to add widgets to my header section so i used register_sidebar function on my functions.php :
function my_theme_add_widget_support()
{
register_sidebar(array(
'id' => 'header-2',
'name' => 'the second hader'
));
}
add_action('widgets_init', 'my_theme_add_widget_support');
And then in the file header.php i write this code in the place where i want the widget to display but it doesn't display anything matter of fact i did a test to see if it's active i found out that it's not active at all even though i make sure that i the widgets area i my admin panel the widget have something inside it :
<?php
if (is_active_sidebar('header-2')) {
echo 'active';
dynamic_sidebar('header-2')
} else {
echo 'not active';
}
?>
this test always return not active
PS: I'm using the old version of widgets editor

Related

Edit php to replace a line of text

I am working on a WordPress development with a theme where a Button has hardcoded text I want to change. I am using a child theme and am hoping there is a way to add some PHP to the functions file to display wording on the button different from the hardcoded text. The theme code is:
if ( ! function_exists( 'boldthemes_get_book_this_tour_button_label' ) ) {
function boldthemes_get_book_this_tour_button_label(){
if ( BoldThemesFrameworkTemplate::$tour_contact_form_booking_show) {
return esc_html__( 'Book this Tour', 'travelicious' );
} else if ( BoldThemesFrameworkTemplate::$tour_contact_form_enquiry_show ) {
return esc_html__( 'Enquiry about the Tour', 'travelicious' );
} else{
return esc_html__( 'Sorry! There are no enabled booking or enquiry forms.', 'travelicious' );
}
}
}
I only need to edit the else if line so that it says 'Enquire about this Trip' instead of 'Enquiry about the Tour'
I have tried a str_replace and it did change the text but threw a conflict error with the theme so am wondering if I either did not set it out correctly or if there is an alternative method? The theme developer recommended adding a translation to the theme but that to me seems a bit heavy just to change a couple of words.
Thanks
One way you could do this is to hook into esc_html like so:
// place in your functions.php
add_filter("esc_html", function($text) {
if ("Enquiry about the Tour" === $text) {
return "Enquire about this Trip";
}
return $text;
});

How to add a virtual page (created by a plugin) to the menu builder

i have been making a plugin which makes it possible to show a schedule in wordpress, but until now i did not find a good way to show the page in wordress.
until now what i did was check if certain querystring variable was set.
but now i found a way to add virtual pages to wordpress:
https://coderwall.com/p/fwea7g/create-wordpress-virtual-page-on-the-fly
i renamed it to virtualClass and used it like this:
new \helpers\VirtualPage(array(
'slug' => '1002',
'post_title' => 'Fake Page Title',
'post content' => 'This is the fake page content'
));
to tryout, i went to the page: mywordpress.site?page_id=1002
this did work, but when i went to the backend in the menu builder, i wont see this page, i want it to be shown there so my users can add the page to their website.
Is there good tutorial on how to do this? is there a hook i can use to add a section to the menu builder?
Like in the image below: add it to either place of the red arrow
Instead of using a virtual page, you should create a page with your plugin's activation and then overwrite that page's content. You will also want to delete
the page on deactivation. In case you want to protect that page from user deletion, I have included that code as well, but it uses and exit; so it's a bit harsh.
function pluginxyz_activate(){
$title = 'Whatever You Want To Call It';
$new_id = wp_insert_post(array(
'post_title'=>$title,
'post_status'=>'publish',
'post_type'=>'page'
));
update_option('pluginxyz_page_id',$new_id);
}
function pluginxyz_deactivate(){
$old_id = get_option('pluginxyz_page_id');
if($old_id)wp_delete_post($old_id,true);
delete_option('pluginxyz_page_id');
}
function pluginxyz_protect_page($post_id){
$old_id = get_option('pluginxyz_page_id');
if($post_id == $old_id)exit('You can not delete this page');
}
function pluginxyz_use_content($content){
$old_id = get_option('pluginxyz_page_id');
global $post;
if($post->ID == $old_id){
//que up your content
} else {
return $content;
}
}
register_activation_hook(__FILE__,'pluginxyz_activate');
register_deactivation_hook( __FILE__, 'pluginxyz_deactivate');
add_action('wp_trash_post', 'pluginxyz_protect_page', 10, 1);
add_action('before_delete_post', 'pluginxyz_protect_page', 10, 1);
add_filter( 'the_content', 'pluginxyz_use_content' );

get file of shortcode wordpress

I am working with wordpress and want to change the output of a shortcode that I wrote myself earlier. Because I forgot where I put it (bad organizing when I started with wp) I wondered whether there is a list of all shortcodes with their source files in the wp database somewhere?
Is there a list that wp uses to connect a shortcode it finds on the page to the fitting source files?
You can use the following code to get a list of all registered shortcodes.
<?php
global $shortcode_tags;
echo '<pre>';
print_r($shortcode_tags);
echo '</pre>';
?>
This will unfortunately not give you the source files of these shortcodes but a simple search through your plugins and themes should reveal the location of the shortcode.
You can print all shortcodes by putting this in a template:
<?php
global $shortcode_tags;
echo "<pre>";
print_r($shortcode_tags);
echo "</pre>";
?>
if you need to include in function.php copy the code to function.php
function add_shortcodes_metaboxes() {
add_meta_box('available_shortcodes', 'Available Shortcodes', 'available_shortcodes', 'page', 'side', 'default');
add_meta_box('available_shortcodes', 'Available Shortcodes', 'available_shortcodes', 'post', 'side', 'default');
}
add_action( 'add_meta_boxes', 'add_shortcodes_metaboxes' );
function available_shortcodes() {
global $shortcode_tags;
foreach ($shortcode_tags as $shortcode_tag => $description) {
echo $shortcode_tag.'<br/>';
};
}

Wordpress list of child sites

i'm trying to show a list of all the sub directories in my multi site network. i've added this function to my theme function.php
function get_all_sites() {
$blog_list = get_blog_list( 0, 'all' );
krsort($blog_list);
foreach ($blog_list AS $blog)
{
echo 'Blog '.$blog['blog_id'].': '.$blog['domain'].$blog['path'].'<br />';
}
}
and added this to the theme header.php
<? get_all_sites(); ?>
But nothing seem to appear. What am i doing wrong?
It looks like is everything okay and it should be working.
I've tested your code here and it's working fine.
But just to advise, you should not be using get_blog_list() it's a deprecated function, since 3.0, you should be using wp_get_sites() instead.
put this code in your functions.php, check your theme, in your question you wrote function.php
function get_all_sites() {
$sites = wp_get_sites();
foreach ($sites as $site) {
printf( 'Blog %d: %s%s <br/>', $site['blog_id'], $site['domain'], $site['path'] );
}
}
Sorry for bad english

Change widget's CSS class names for different category pages in WordPress

I want to change the class names of the sidebar widgets on every different category page of WordPress and I figured the best way to do this would be to make a function in functions.php with all the conditions and return the required class name. I then called the function in the list tags of the register_sidebar function.
if (function_exists('register_sidebar')) {
register_sidebar(array(
'before_widget' => '<li class="sidebarModule">',
'after_widget' => '</li><!-- end module -->',
'before_title' => '<h2 class="moduleTitle "'.set_widget_title_color().'>',
'after_title' => '</h2>',
));
}
function set_widget_title_color() {
if(is_category('technology')) {
$color = "catNavColor1_active";
} elseif(is_category('gadgets')) {
$color = "catNavColor2_active";
} elseif(is_category('social-media')) {
$color = "catNavColor3_active";
} elseif(is_category('gaming')) {
$color = "catNavColor4_active";
} elseif(is_category('other')) {
$color = "catNavColor5_active";
}
return $color;
}
For some reason the above doesn't work. Please Help
Thanks
I think register_sidebars is called too early in the process, when the category is not defined yet. Have you tried implementing the dynamic_sidebar_params filter? I could not find anything in the WordPress Codex, but I found this nice example. Your other question on this topic also has a complete answer.
This only works if you implement the sidebars using dynamic_sidebar in your widget, as this function calls the dynamic_sidebar_params filter. If you have static widgets (defined in your template, not using the widget admin page), you should add a call to your function in that template code, like this:
<li class="sidebarModule">
<h2 class="moduleTitle <?php echo set_widget_title_color(); ?>">Widget title</h2>
<?php /* Your widget code */ ?>
</li><!-- end module -->

Categories