how to populate widgets on sidebar on theme activation - wordpress

What I am trying to do is pre-populate the sidebar widget area with some default widgets on theme activation.
if ( ! dynamic_sidebar( 'sidebar' ) ) :
does add the widgets but it doesnot show up in the sidebar of widgets section and
if ( is_active_sidebar( 'sidebar' ) ) {
this function doesnot work if the widgets are not loaded in the sidebar widgetized area.
I know it is possible but I am just out of idea. I googled but didnot find any solutions. Thank you for any help in advance.

It isn't clear from your answer if you use the after_switch_theme hook but that the moment you need to set the widgets.
To activate the widgets I suggest writing it directly into the database with get_option('sidebars_widgets') which should give an array, and save it with update_option('sidebars_widgets', $new_activated_widgets).
This should help you get started.
/**
* set new widgets on theme activate
* #param string $old_theme
* #param WP_Theme $WP_theme
*/
function set_default_theme_widgets ($old_theme, $WP_theme = null) {
// check if the new theme is your theme
// figure it out
var_dump($WP_theme);
// the name is (probably) the slug/id
$new_active_widgets = array (
'sidebar-name' => array (
'widget-name-1',
'widget-name-2',
'widget-name-3',
),
'footer-sidebar' => array(
'widget-name-1',
'widget-name-2',
'widget-name-3',
)
);
// save new widgets to DB
update_option('sidebars_widgets', $new_active_widgets);
}
add_action('after_switch_theme', 'set_default_theme_widgets', 10, 2);
Tested, just paste it in functions.php of your theme.

If anyone else needed to know how to add multiple default widgets (different instances) to multiple sidebars at the same time, the following code will add the widgets both to the page and under the admin widget tab. I realize that this may have been obvious to everyone but me.
So based on janw and kcssm's hard work:
function add_theme_widgets($old_theme, $WP_theme = null) {
$activate = array(
'right-sidebar' => array(
'recent-posts-1',
'categories-1',
'archives-1'
),
'footer-sidebar' => array(
'recent-posts-2',
'categories-2',
'archives-2'
)
);
/* the default titles will appear */
update_option('widget_recent-posts', array(
1 => array('title' => ''),
2 => array('title' => '')));
update_option('widget_categories', array(
1 => array('title' => ''),
2 => array('title' => '')));
update_option('widget_archives', array(
1 => array('title' => ''),
2 => array('title' => '')));
update_option('sidebars_widgets', $activate);
}
add_action('after_switch_theme', 'add_theme_widgets', 10, 2);
This will however delete any other settings, so tread carefully!

Related

Rename plugin name in Wordpress Dashboard Menu

I have tried a lot of suggestions on different posts here, but none of them seem to work for me.
I want to rename the name of a plugin on the dashboard menu on my Wordpress site. The name of the plugin is Sensei LMS and the path of the plugin folder is /plugins/sensei-lms.
I'd appreciate if someone helps me with some code that I can use to rename this. Thank you
I've used this tutorial in the past.
In your case, it would be:
function my_renamed_admin_menu_items() {
global $menu;
// Define your changes here
$updates = array(
"Sensei LMS" => array(
'name' => 'New Menu Name'
),
"Another Menu Name" => array(
'name' => 'Another New Name'
)
);
foreach ($menu as $k => $props) {
// Check for new values
$new_values = (isset($updates[$props[0]])) ? $updates[$props[0]] : false;
if (!$new_values) continue;
// Change menu name
$menu[$k][0] = $new_values['name'];
}
}
add_action('admin_init', 'my_renamed_admin_menu_items');

WPBakery Page Builder - Checkboxes won't retain values from v6.xx

For the last few years I have been developing a custom theme and various plugins that add options to WPBakery Page Builder elements and create new custom elements, etc.
Everything has worked perfectly up to Page Builder v5.7. But then when v6.0.x came out, suddenly every Checkbox option I have added to every element, be it a custom element or one of Page Builder's standard elements, has a problem: As soon as I click the element in the Page Editor to open it's settings, all checkboxes clear and become unticked regardless of their default or saved values.
If I tick a checkbox and save settings, when I view the front end of the site the option has indeed saved and is working as it should be; but when I open the element's settings window again, they all clear.
I have looked at all my code and compared it to other plugin's code and all of WPBakery's documentation, etc, and I can't see a problem anywhere. I thought it might be a bug with Page Builder v6.0.x and I have sent them a support ticket, but they haven't been able to give me an answer. Other people's plugins seem to be working though, along with all of Page Builder's own built-in elements, which leads me to believe that it must be something to do with my code.
Here is an example:
// After VC Init
add_action( 'vc_after_init', 'gd_after_init_actions' );
// ADD OPTIONS TO VISUAL COMPOSER ELEMENTS //
function gd_after_init_actions() {
// ADD FULL WIDTH CHECKBOX TO SINGLE IMAGE ELEMENTS //
$single_image_attributes = array(
'type' => 'checkbox',
'class' => 'full_width_image',
'param_name' => 'full_width_image',
'value' => array('Force Full Width' => true),
'weight' => 1
);
vc_add_param('vc_single_image', $single_image_attributes);
}
This adds a "Force Full Width" checkbox to Page Builder's standard "Single Image" element. The checkbox appears perfectly and if I check it, save and then view the front end of the site, the checkbox does indeed work; the checkbox value has saved and the image is stretched to full width. But then if I go back to the back end and click on the Single Image element to edit it's settings again, the checkbox becomes unchecked and it's saved value is lost.
Similarly, with plugins in which I am adding options with vc_map(), here is a sample:
function vc_before_init_actions(){
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
// ELEMENT CLASS //
class vcResponsiveYouTubeVideo extends WPBakeryShortCode {
// ELEMENT INIT //
function __construct() {
add_action( 'init', array( $this, 'vc_youtube_video_mapping' ) );
add_shortcode( 'vc_youtube', array( $this, 'vc_youtube_video_html' ) );
}
// ELEMENT MAPPING //
public function vc_youtube_video_mapping() {
// Map the block with vc_map()
$youtubeIcon = plugins_url('responsive-youtube-icon.png',__FILE__ );
vc_map(
array(
'name' => __('Responsive YouTube Video', 'text-domain'),
'base' => 'vc_youtube',
'category' => __('Custom Elements', 'text-domain'),
'icon' => $youtubeIcon,
'params' => array(
array(
'type' => 'checkbox',
'param_name' => 'lightbox',
'value' => array('Pop up video in lightbox' => true),
'admin_label' => false,
'weight' => 0,
'group' => 'Custom Group'
),
)
)
);
}
[etc...]
}
This is just a snippet of the code and there is much more to it, but this is just the part pertaining to one of the checkbox options that I have in this particular plugin. Just like the previous example, if I check it and save the options, it works, but then if I come back into the element's settings, it clears.
Even checkboxes that are set to be checked by default:
array(
'type' => 'checkbox',
'param_name' => 'branding',
'value' => array('Keep YouTube Branding in Control Bar' => true),
'std' => true,
'admin_label' => false,
'weight' => 0,
'group' => 'Custom Group'
),
This checkbox still clears as soon as I click open the settings window.
I've done a fair bit of testing and it appears this issue exists (so far) only with Checkboxes. All the other inputs I've created, such as textfields, colorpickers, dropdowns, etc, all save fine and retain their values when I re-open the settings window.
Does anyone any have ideas on this?
we found an issue, it caused because we added strict compare for key-value pairs for checkboxes, but in your mapping value is boolean true, however shortcode saves this as string "1" and when edit form is opening then strict compare fails.
For BC we will fix this in our next release, there is a patch:
Index: include/params/default_params.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- include/params/default_params.php (revision afcb0ccf521c36ce176651964792d18c1f9b1bfc)
+++ include/params/default_params.php (date 1559032145000)
## -85,7 +85,9 ##
$values = isset( $settings['value'] ) && is_array( $settings['value'] ) ? $settings['value'] : array( esc_html__( 'Yes', 'js_composer' ) => 'true' );
if ( ! empty( $values ) ) {
foreach ( $values as $label => $v ) {
- $checked = in_array( $v, $current_value, true ) ? 'checked' : '';
+ // NOTE!! Don't use strict compare here for BC!
+ // #codingStandardsIgnoreLine
+ $checked = in_array( $v, $current_value ) ? 'checked' : '';
$output .= ' <label class="vc_checkbox-label"><input id="' . $settings['param_name'] . '-' . $v . '" value="' . $v . '" class="wpb_vc_param_value ' . $settings['param_name'] . ' ' . $settings['type'] . '" type="checkbox" name="' . $settings['param_name'] . '" ' . $checked . '>' . $label . '</label>';
}
}

Custom sidebars disappearing with Wordpress 4.4 update

Do you have any initial ideas as to why custom sidebars would stop displaying in a Wordpress theme after update 4.4? Our login/join page showed a custom sidebar with the login/join form on the right of the page and other up until the update. This is not a custom sidebar plugin, it's part of the theme. Any thoughts? MANY thanks!
The default sidebar can be enabled, but any custom created sidebars are rendered blank.
Code registering the sidebar within functions.php:
include('includes/sidebar/sidebar.php');
Code within sidebar.php:
include('functions/custom-sidebars.php');
Full code from custom-sidebars.php:
`//// GETS OUR CUSTOM SIDEBARS
$sidebars = get_option('dd_custom_sidebars');
//// IF THEY EXIST
if($sidebars) {
//// LOOPS AND REGISTERS THEM
foreach($sidebars as $sidebar) {
$args = array(
'name' => $sidebar.' — Custom',
'id' => us_get_sidebar_id($sidebar),
'description' => $sidebar.' — Custom Sidebar',
'before_widget' => '<div class="sidebar-item">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>',
);
//// REGISTERS IT
register_sidebar($args);
}
}
/// FUNCTION TO GENERATE OUR FRIENDLY NAME
function us_get_sidebar_id($phrase) {
$result = strtolower($phrase);
$result = preg_replace("/[^a-z0-9\s-]/", "", $result);
$result = trim(preg_replace("/[\s-]+/", " ", $result));
$result = trim(substr($result, 0, 20));
$result = preg_replace("/\s/", "-", $result);
return $result;
}`
I had the same problem because my code that was creating custom sidebars was wrong in my sidebar.php file.
I was using this …
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Block 3') ) : ?>
Instead of this
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('block-3') ) : ?>
which is the id of the sidebar from my functions.php file
As a usable workaround (but not an answer to why the custom sidebar code is not working) I'm now dumping all the various widgets into the default sidebar which works and using the plugin Display Widgets to determine which page the widgets are shown on.

Adding User Profile Fields to Wordpress Buddypress to Include "Favorites"

I'm trying to figure out a way for members on my Wordpress (Buddypress) site to pick their "favorite movies, books, etc."
It would be nice if, instead of members simply typing a list of these things, they could select from books already in the system, and add more as the please in the future.
I'm hoping that there is an easy answer to this, such as a plugin that I can use, or, at least, modify. Does anyone know of anything that I can look into?
Your title asks how to add a user profile field. Here is the code to add as many fields as you like. Once you add the field, you can easily place additional inputs or options on the custom tab page for users to enter their own favorites.
function my_test_setup_nav() {
global $bp;
$parent_slug = ‘test’;
$child_slug = ‘test_sub’;
//name, slug, screen, position, default subnav
bp_core_new_nav_item( array(‘name’ => __( ‘Test’ ),’slug’ => $parent_slug,’screen_function’ => ‘my_profile_page_function_to_show_screen’,'position’ => 40,’default_subnav_slug’ => $child_slug ) );
/* Add the subnav items to the profile */
// name, slug, parent_url, parent slug, screen function
bp_core_new_subnav_item( array( ‘name’ => __( ‘Home’ ), ‘slug’ => $child_slug, ‘parent_url’ => $bp->loggedin_user->domain . $parent_slug.’/', ‘parent_slug’ => $parent_slug, ‘screen_function’ => ‘my_profile_page_function_to_show_screen’ ) );
bp_core_new_subnav_item( array( ‘name’ => __( ‘Random Page’ ), ‘slug’ => ‘random’, ‘parent_url’ => $bp->loggedin_user->domain . $parent_slug.’/', ‘parent_slug’ => $parent_slug, ‘screen_function’ => ‘my_profile_page_function_to_show_screen234′ ) );
}
function my_profile_page_function_to_show_screen() {
//add title and content here – last is to call the members plugin.php template
add_action( ‘bp_template_title’, ‘my_profile_page_function_to_show_screen_title’ );
add_action( ‘bp_template_content’, ‘my_profile_page_function_to_show_screen_content’ );
bp_core_load_template( apply_filters( ‘bp_core_template_plugin’, ‘members/single/plugins’ ) );
}
function my_profile_page_function_to_show_screen_title() {
echo ‘wptaskforce title’;
}
function my_profile_page_function_to_show_screen_content() {
echo ‘wptaskforce content’;
}
//random page content:
function my_profile_page_function_to_show_screen234() {
//add content here – last is to call the members plugin.php template
add_action( ‘bp_template_content’, ‘my_profile_page_function_to_show_screen234_content’ );
bp_core_load_template( apply_filters( ‘bp_core_template_plugin’, ‘members/single/plugins’ ) );
}
function my_profile_page_function_to_show_screen234_content() {
echo ‘This is a random page.’;
}
add_action( ‘bp_setup_nav’, ‘my_test_setup_nav’ );

How to display custom html block in Drupal using theme?

Having some module, defined some url in hook_menu() and need to display there some theme (modules/mymodule/templates/mytheme.tpl.php).
How do I show mytheme.tpl.php content on needed url?
function mymodule_menu(){
$item = array();
$item['somemenu'] = array(
'page callback' => 'somemenu_display',
);
return $item;
}
function somemenu_display(){
return WHAT_IS_THIS_FUNCTION('modules/mymodule/templates/mytheme.tpl.php');
}
And it will be good to display only these contents, without and header/footer.
The function is Theme()
return theme('some_theme_function_template', array('aValues' => $someArray));
You then need to use the theme hook like this:
function my_module_name_theme() {
return array(
'some_theme_function_template' => array(
'template' => 'mytheme',
),
);
}
It now searches for mytheme.tpl.php in the root of your module.

Resources