web-dev-qa-db-ja.com

テーマのアクティブ化でカスタムサイドバーにカスタムウィジットを挿入する方法

私はカスタムウィジェットを作成しました、そして私はそれを私がテーマ活性化で欲しかったどんなサイドバーにでも挿入したいです。出来ますか?

ところで、これが私のサイドバーに使用したコードです。

function awesome_register_sidebars() {
    register_sidebar(
        array(
            'id' => 'sidebar-footer-1',
            'name' => __( 'Footer Nav  Links 1', 'Awesome' ),
            'description' => __( 'A short description of the sidebar.', 'Awesome' ),
            'before_widget' => '<div class="footer-link">',
            'after_widget' => '</div>',
            'before_title' => '<h3 class="footer-link-head">',
            'after_title' => '</h3>'
                ));
register_sidebar(
        array(
            'id' => 'sidebar-footer-2',
            'name' => __( 'Footer Nav Links 2', 'Awesome' ),
            'description' => __( 'A short description of the sidebar.', 'Awesome' ),
            'before_widget' => '<div class="footer-link">',
            'after_widget' => '</div>',
            'before_title' => '<h3 class="footer-link-head">',
            'after_title' => '</h3>'
                ));
register_sidebar(
        array(
            'id' => 'sidebar-footer-3',
            'name' => __( 'Footer Nav  Links 3', 'Awesome' ),
            'description' => __( 'A short description of the sidebar.', 'Awesome' ),
            'before_widget' => '<div class="footer-link">',
            'after_widget' => '</div>',
            'before_title' => '<h3 class="footer-link-head">',
            'after_title' => '</h3>'
                ));
}
add_action( 'widgets_init', 'awesome_register_sidebars' );

そして、これが私がウィジェットに使用したコードです:

<?php

class footerlinks_widget extends WP_Widget
{
    // Create Multiple WordPress Widgets
    function __construct()
    {
        parent::__construct('footerlinks_widget', __('Footer Links', 'footer_menu_domain'), array(
            'description' => __('Adds menu and links to footer', 'footer_menu_domain')
        ));
    }

    public function widget($args, $instance)
    {
        $foot_nav_1       = apply_filters('foot_nav_1', $instance['foot_nav_1']);
        $url_link_1       = apply_filters('url_link_1', $instance['url_link_1']);
        $url_name_1       = apply_filters('url_name_1', $instance['url_name_1']);
        $url_link_2       = apply_filters('url_link_2', $instance['url_link_2']);
        $url_name_2       = apply_filters('url_name_2', $instance['url_name_2']);
        $url_link_3       = apply_filters('url_link_3', $instance['url_link_3']);
        $url_name_3       = apply_filters('url_name_3', $instance['url_name_3']);
        $url_link_4       = apply_filters('url_link_4', $instance['url_link_4']);
        $url_name_4       = apply_filters('url_name_4', $instance['url_name_4']);
        $url_link_5       = apply_filters('url_link_5', $instance['url_link_5']);
        $url_name_5       = apply_filters('url_name_5', $instance['url_name_5']);
        $url_link_6       = apply_filters('url_link_6', $instance['url_link_6']);
        $url_name_6       = apply_filters('url_name_6', $instance['url_name_6']);
        $url_link_7       = apply_filters('url_link_7', $instance['url_link_7']);
        $url_name_7       = apply_filters('url_name_7', $instance['url_name_7']);
        $open_links_newtab  = $instance['open_links_newtab'] ? 'true' : 'false';
        if($open_links_newtab == "true") {
        $newtablink = 'target="_blank"';
        }
        $footermenu = '<h3 class="footer-link-head test">'.$foot_nav_1.'</h3> 
        <p><a title="'.$url_name_1.'" href="'.$url_link_1.'"'.$newtablink.'">'.$url_name_1.'</a></p> 
        <p><a title="'.$url_name_2.'" href="'.$url_link_2.'"'.$newtablink.'>'.$url_name_2.'</a></p> 
        <p><a title="'.$url_name_3.'" href="'.$url_link_3.'"'.$newtablink.'>'.$url_name_3.'</a></p> 
        <p><a title="'.$url_name_4.'" href="'.$url_link_4.'"'.$newtablink.'>'.$url_name_4.'</a></p> 
        <p><a title="'.$url_name_5.'" href="'.$url_link_5.'"'.$newtablink.'>'.$url_name_5.'</a></p> 
        <p><a title="'.$url_name_6.'" href="'.$url_link_6.'"'.$newtablink.'>'.$url_name_6.'</a></p> 
        <p><a title="'.$url_name_7.'" href="'.$url_link_7.'"'.$newtablink.'>'.$url_name_7.'</a></p>';

        echo $before_widget;
        echo $before_title . $after_title;
        echo $footermenu;
        echo $after_widget;
    }

    // Create Instance and Assign Values
    public function form($instance)
    {
        if (isset($instance['foot_nav_1'])) {
            $foot_nav_1 = $instance['foot_nav_1'];
        } else {
            $foot_nav_1 = __('Navigation', 'footer_menu_domain');
        }
            if (isset($instance['url_link_1'])) {
            $url_link_1 = $instance['url_link_1'];
        }
        if (isset($instance['url_name_1'])) {
            $url_name_1 = $instance['url_name_1'];
        }
        else {
            $url_name_1 = __('Movies', 'footer_menu_domain');
        }
        if (isset($instance['url_link_2'])) {
            $url_link_2 = $instance['url_link_2'];
        }
        if (isset($instance['url_name_2'])) {
            $url_name_2 = $instance['url_name_2'];
        }
        else {
            $url_name_2 = __('Top IMDb', 'footer_menu_domain');
        }
        if (isset($instance['url_link_3'])) {
            $url_link_3 = $instance['url_link_3'];
        }
        if (isset($instance['url_name_3'])) {
            $url_name_3 = $instance['url_name_3'];
        }
        else {
            $url_name_3 = __('DMCA', 'footer_menu_domain');
        }
        if (isset($instance['url_link_4'])) {
            $url_link_4 = $instance['url_link_4'];
        }
        if (isset($instance['url_name_4'])) {
            $url_name_4 = $instance['url_name_4'];
        }
        else {
            $url_name_4 = __('FAQ', 'footer_menu_domain');
        }
        if (isset($instance['url_link_5'])) {
            $url_link_5 = $instance['url_link_5'];
        }
        if (isset($instance['url_name_5'])) {
            $url_name_5 = $instance['url_name_5'];
        }
        else {
            $url_name_5 = __('News', 'footer_menu_domain');
        }
        if (isset($instance['url_link_6'])) {
            $url_link_6 = $instance['url_link_6'];
        }
        if (isset($instance['url_name_6'])) {
            $url_name_6 = $instance['url_name_6'];
        }
        else {
            $url_name_6 = __('Request', 'footer_menu_domain');
        }
        if (isset($instance['url_link_7'])) {
            $url_link_7 = $instance['url_link_7'];
        }
        if (isset($instance['url_name_7'])) {
            $url_name_7 = $instance['url_name_7'];
        }
        else {
            $url_name_7 = __('Contact', 'footer_menu_domain');
        }
        $instance['open_links_newtab']  = $instance['open_links_newtab'] ? 'true' : 'false';

?>

<p>

    <label for="<?php echo $this->get_field_id('foot_nav_1'); ?>"> <?php _e('Navigation Name');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('foot_nav_1');?>" name="<?php echo $this->get_field_name('foot_nav_1');?>" type="text" value="<?php echo esc_attr($foot_nav_1);?>" />
     <h4>Link #1</h4>
 <hr>
    <label for="<?php echo $this->get_field_id('url_link_1'); ?>"> <?php _e('URL');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_link_1');?>" name="<?php echo $this->get_field_name('url_link_1');?>" type="text" value="<?php echo esc_attr($url_link_1);?>" />

    <label for="<?php echo $this->get_field_id('url_name_1'); ?>"> <?php _e('Name');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_name_1');?>" name="<?php echo $this->get_field_name('url_name_1');?>" type="text" value="<?php echo esc_attr($url_name_1);?>" />
 <h4>Link #2</h4>
 <hr>
    <label for="<?php echo $this->get_field_id('url_link_2'); ?>"> <?php _e('URL');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_link_2');?>" name="<?php echo $this->get_field_name('url_link_2');?>" type="text" value="<?php echo esc_attr($url_link_2);?>" />

    <label for="<?php echo $this->get_field_id('url_name_2'); ?>"> <?php _e('Name');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_name_2');?>" name="<?php echo $this->get_field_name('url_name_2');?>" type="text" value="<?php echo esc_attr($url_name_2);?>" />
 <h4>Link #3</h4>
 <hr>
    <label for="<?php echo $this->get_field_id('url_link_3'); ?>"> <?php _e('URL');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_link_3');?>" name="<?php echo $this->get_field_name('url_link_3');?>" type="text" value="<?php echo esc_attr($url_link_3);?>" />

    <label for="<?php echo $this->get_field_id('url_name_3'); ?>"> <?php _e('Name');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_name_3');?>" name="<?php echo $this->get_field_name('url_name_3');?>" type="text" value="<?php echo esc_attr($url_name_3);?>" />
 <h4>Link #4</h4>
 <hr>
    <label for="<?php echo $this->get_field_id('url_link_4'); ?>"> <?php _e('URL');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_link_4');?>" name="<?php echo $this->get_field_name('url_link_4');?>" type="text" value="<?php echo esc_attr($url_link_4);?>" />

    <label for="<?php echo $this->get_field_id('url_name_4'); ?>"> <?php _e('Name');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_name_4');?>" name="<?php echo $this->get_field_name('url_name_4');?>" type="text" value="<?php echo esc_attr($url_name_4);?>" />
 <h4>Link #5</h4>
 <hr>
    <label for="<?php echo $this->get_field_id('url_link_5'); ?>"> <?php _e('URL');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_link_5');?>" name="<?php echo $this->get_field_name('url_link_5');?>" type="text" value="<?php echo esc_attr($url_link_5);?>" />

    <label for="<?php echo $this->get_field_id('url_name_5'); ?>"> <?php _e('Name');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_name_5');?>" name="<?php echo $this->get_field_name('url_name_5');?>" type="text" value="<?php echo esc_attr($url_name_5);?>" />
    <h4>Link #6</h4>
 <hr>
    <label for="<?php echo $this->get_field_id('url_link_6'); ?>"> <?php _e('URL');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_link_6');?>" name="<?php echo $this->get_field_name('url_link_6');?>" type="text" value="<?php echo esc_attr($url_link_6);?>" />

    <label for="<?php echo $this->get_field_id('url_name_6'); ?>"> <?php _e('Name');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_name_6');?>" name="<?php echo $this->get_field_name('url_name_6');?>" type="text" value="<?php echo esc_attr($url_name_6);?>" />
    <h4>Link #7</h4>
 <hr>
    <label for="<?php echo $this->get_field_id('url_link_7'); ?>"> <?php _e('URL');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_link_7');?>" name="<?php echo $this->get_field_name('url_link_7');?>" type="text" value="<?php echo esc_attr($url_link_7);?>" />

    <label for="<?php echo $this->get_field_id('url_name_7'); ?>"> <?php _e('Name');?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('url_name_7');?>" name="<?php echo $this->get_field_name('url_name_7');?>" type="text" value="<?php echo esc_attr($url_name_7);?>" />
    <br /> <br /> 

    <label for="<?php echo $this->get_field_id('open_links_newtab');?>"><?php _e('Open links in new tab'); ?></label>
    <input class="checkbox" type="checkbox" <?php checked($instance['open_links_newtab'], 'true'); ?> id="<?php echo $this->get_field_id('open_links_newtab'); ?>" name="<?php echo $this->get_field_name('open_links_newtab'); ?>" />

</p>

<?php
    }

    // Updating widget replacing old instances with new
    function update($new_instance, $old_instance)
    {
        $instance                           = array();
        $instance['foot_nav_1'] = (!empty($new_instance['foot_nav_1'])) ? strip_tags($new_instance['foot_nav_1']) : '';
        $instance['url_link_1'] = (!empty($new_instance['url_link_1'])) ? strip_tags($new_instance['url_link_1']) : '';
        $instance['url_name_1'] = (!empty($new_instance['url_name_1'])) ? strip_tags($new_instance['url_name_1']) : '';
        $instance['url_link_2'] = (!empty($new_instance['url_link_2'])) ? strip_tags($new_instance['url_link_2']) : '';
        $instance['url_name_2'] = (!empty($new_instance['url_name_2'])) ? strip_tags($new_instance['url_name_2']) : '';
        $instance['url_link_3'] = (!empty($new_instance['url_link_3'])) ? strip_tags($new_instance['url_link_3']) : '';
        $instance['url_name_3'] = (!empty($new_instance['url_name_3'])) ? strip_tags($new_instance['url_name_3']) : '';
        $instance['url_link_4'] = (!empty($new_instance['url_link_4'])) ? strip_tags($new_instance['url_link_4']) : '';
        $instance['url_name_4'] = (!empty($new_instance['url_name_4'])) ? strip_tags($new_instance['url_name_4']) : '';
        $instance['url_link_5'] = (!empty($new_instance['url_link_5'])) ? strip_tags($new_instance['url_link_5']) : '';
        $instance['url_name_5'] = (!empty($new_instance['url_name_5'])) ? strip_tags($new_instance['url_name_5']) : '';
        $instance['url_link_6'] = (!empty($new_instance['url_link_6'])) ? strip_tags($new_instance['url_link_6']) : '';
        $instance['url_name_6'] = (!empty($new_instance['url_name_6'])) ? strip_tags($new_instance['url_name_6']) : '';
        $instance['url_link_7'] = (!empty($new_instance['url_link_7'])) ? strip_tags($new_instance['url_link_7']) : '';
        $instance['url_name_7'] = (!empty($new_instance['url_name_7'])) ? strip_tags($new_instance['url_name_7']) : '';
        $instance['open_links_newtab']  = $new_instance['open_links_newtab'];
        return $instance;
    }
}

function footer_menu()
{
    register_widget('footerlinks_widget');
}

// Initialize Plugin
add_action('widgets_init', 'footer_menu');

?>
1
Archangel17

しかし、テーマがアクティブになったときにこれをしたいのであれば、ちょっとしたことを変更しなければなりません。

function awesome_register_sidebars() {
      $sidebars = array ( 'a' => 'sidebar-footer-1', 'b' => 'sidebar- footer-2','c'=>'sidebar-footer-3' );
foreach ($sidebars as $sidebar) {
    register_sidebar(
        array (
            'name'          => $sidebar,
            'id'            => $sidebar,
            'before_widget' => '',
            'after_widget'  => ''
        )
    );
}
$active_widgets = get_option('sidebars_widgets');

if (! empty($active_widgets[ $sidebars['a'] ])
    or ! empty($active_widgets[ $sidebars['b'] ])
) {
    return;
}

$counter = 1;
$active_widgets[ $sidebars['a'] ][0] = 'footerlinks_widget-' . $counter;
$demo_widget_content[ $counter ] = array ( 'text' => "This works!\n\nAmazing!" );

$counter++;


$active_widgets[ $sidebars['a'] ][] = 'footerlinks_widget-' . $counter;

$footerlinks_widget_content[ $counter ] = array ();
update_option('widget_footerlinks_widget', $footerlinks_widget_content);

 $counter++;


   update_option('sidebars_widgets', $active_widgets);
 }
add_action( 'widgets_init', 'awesome_register_sidebars' );
1
Sonali