Bagaimana Anda memanggil kode pendek di widget di wordpress?

Plugin ini memungkinkan penyisipan 1 atau "set" widget melalui kode pendek ke halaman, posting, (dan jenis posting Kustom?) Dan itu juga akan menyimpan daftar kode pendek yang dikonfigurasi ~ set. Sangat berguna

Saat membandingkan fitur-fiturnya dengan plugin Widgets Shortcode, plugin tersebut tidak menyediakan sarana untuk menyimpan "kumpulan" plugin, melainkan tampaknya berfokus pada penyisipan widget satu per satu. Faktanya, ini menyisipkan kode pendek untuk setiap widget di tepi bawah setiap widget dan menetapkan kode pendek yang berbeda untuk setiap contoh widget. Juga bagus

https. //wordpress. org/plugins/widget-kode pendek/

Saya suka granularity Widgets Shortcode menyediakan dengan shortcode individu untuk setiap instance dari setiap widget. Saat menjalankannya dan plugin Anda secara bersamaan, bahkan widget yang disimpan di "set" plugin Anda masih diberi kode pendek individual. Kode pendek individu tersebut memungkinkan penggunaan widget tertentu dari "set" di lokasi yang berbeda tanpa perlu membuat instance widget tambahan karena tampaknya diperlukan dengan plugin Anda

Kebetulan, bagi siapa saja yang menginginkan kedua fitur ini, kedua plugin ini bekerja dengan sangat baik di salah satu WP 4 kami. 0 Instalasi multisite dengan 50+ situs dan bahkan di situs yang menggunakan tema Make

FYI. Kami juga menguji amr shortcode widget apa pun, tetapi mengalami beberapa masalah dengannya dan alih-alih memecahkan masalah, kami melanjutkan. Agar adil, kami menguji dengan cepat di situs dengan lebih dari 160 plugin aktif. Ya, 160 plugin. Aku tahu, aku tahu, tapi pikirkanlah sebentar. Plugin apa pun yang dapat Anda aktifkan ke dalam campuran 160 plugin itu dan tidak merusak atau menimbulkan masalah, maka itu menunjukkan kemungkinan besar itu mudah digunakan. setidaknya sampai putaran pembaruan WP berikutnya. . )

Kami juga menguji Widgetize Pages Light dan Sidebar & Widget Manager versi pro dan tidak ada masalah dengan keduanya. Namun, jika hanya ingin memasukkan widget ke halaman dan posting, plugin ini sepertinya berlebihan karena menyediakan fungsi tata letak halaman tambahan yang dapat menduplikasi fungsi tema dan/atau fungsi plugin lainnya.

Saya sedang mencari plugin "widget di halaman" yang menyediakan tombol TinyMCE untuk memasukkan kode pendek widget

Adakah yang bisa merekomendasikan yang memiliki tombol TinyMCE untuk memasukkan kode pendek widget di halaman, posting, dan CPT?

Akhirnya saya memilih Widgets On Pages karena memiliki kombinasi fitur terbaik untuk aplikasi ini. Sederhana untuk dipahami, mudah digunakan, serbaguna, dan telah bekerja dengan sempurna bagi kami, bahkan di samping Kode Pendek Widget

Kami dapat melakukan banyak hal berbeda dengan kode pendek di WordPress, seperti yang kami jelajahi di artikel kami yang menjelaskan API Kode Pendek WordPress. Namun, secara default, WordPress hanya mengizinkan penggunaan shortcode di postingan (dan halaman), dan tidak di tempat lain

Jika Anda ingin menggunakan kode pendek di widget, itu tidak mungkin dilakukan secara default. Namun, dalam tip singkat ini, saya akan membahas bagaimana Anda dapat mengaktifkan fungsi ini

Bagaimana Anda memanggil kode pendek di widget di wordpress?

Mengizinkan Penggunaan Kode Pendek di Widget Teks

WordPress menyediakan beberapa widget secara default. Salah satunya adalah widget 'Teks', yang, seperti namanya, memungkinkan Anda menambahkan teks apa pun ke widget. Anda juga dapat menggunakannya untuk menambahkan kode HTML apa pun

Itu artinya Anda juga bisa bermain dengan kode JavaScript di widget ini, jadi ini cukup hebat. Namun, jika Anda memerlukan lebih banyak, seperti skrip PHP untuk mengakses beberapa data yang disimpan di server, widget ini tidak akan membantu Anda secara default.

Ini mirip dengan postingan. Itu sebabnya, seperti di postingan, kami ingin dapat menggunakan kode pendek untuk melakukan apa pun yang kami inginkan. Untuk melakukannya, kita dapat menggunakan filter widget_text. Filter ini dipanggil untuk mengizinkan modifikasi pada konten widget 'Teks'. Kami akan menggunakannya di sini untuk meminta WordPress mengurai kode pendek di widget ini

Mem-parsing kode pendek di WordPress dicapai berkat fungsi do_shortcode(). Itu menerima satu parameter yang diperlukan, teks untuk diurai, dan mengembalikan teks yang diuraikan. Artinya, kita bisa langsung menggunakan fungsi ini sebagai fungsi callback di filter widget_text

Kode di bawah ini dapat digunakan dalam file plugin atau dalam file functions.php tema Anda

<?php
add_filter('widget_text', 'do_shortcode');
?>

Dan kita sudah selesai. Sekarang, kode pendek apa pun yang Anda ketik di widget 'Teks' akan diuraikan

Bagaimana Anda memanggil kode pendek di widget di wordpress?

Membuat Widget Kode Pendek Baru

Alternatifnya, kita juga bisa membuat widget sendiri. Karena widget 'Teks' default berfungsi dengan baik, kita cukup mengadaptasi kodenya dari berikut ini (ditemukan di file /wp-includes/default-widgets.php). Perlu dicatat bahwa kita harus membuat plugin kita sendiri, jangan pernah memodifikasi file inti WordPress

<?php
class WP_Widget_Text extends WP_Widget {

    public function __construct() {
        $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML.'));
        $control_ops = array('width' => 400, 'height' => 350);
        parent::__construct('text', __('Text'), $widget_ops, $control_ops);
    }

    /**
     * @param array $args
     * @param array $instance
     */
    public function widget( $args, $instance ) {
        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        /**
         * Filter the content of the Text widget.
         *
         * @since 2.3.0
         *
         * @param string    $widget_text The widget content.
         * @param WP_Widget $instance    WP_Widget instance.
         */
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
        echo $args['before_widget'];
        if ( ! empty( $title ) ) {
            echo $args['before_title'] . $title . $args['after_title'];
        } ?>
            <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
        <?php
        echo $args['after_widget'];
    }

    /**
     * @param array $new_instance
     * @param array $old_instance
     * @return array
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        if ( current_user_can('unfiltered_html') )
            $instance['text'] =  $new_instance['text'];
        else
            $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
        $instance['filter'] = ! empty( $new_instance['filter'] );
        return $instance;
    }

    /**
     * @param array $instance
     */
    public function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
        $title = strip_tags($instance['title']);
        $text = esc_textarea($instance['text']);
?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>

        <p><label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label>
        <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea></p>

        <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
    }
}
?>
_

Kami tidak memiliki banyak detail untuk diubah di sini. Hal pertama yang harus diubah adalah nama kelas. Saya memilih untuk menamainya

<?php
class WP_Widget_Text extends WP_Widget {

    public function __construct() {
        $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML.'));
        $control_ops = array('width' => 400, 'height' => 350);
        parent::__construct('text', __('Text'), $widget_ops, $control_ops);
    }

    /**
     * @param array $args
     * @param array $instance
     */
    public function widget( $args, $instance ) {
        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        /**
         * Filter the content of the Text widget.
         *
         * @since 2.3.0
         *
         * @param string    $widget_text The widget content.
         * @param WP_Widget $instance    WP_Widget instance.
         */
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
        echo $args['before_widget'];
        if ( ! empty( $title ) ) {
            echo $args['before_title'] . $title . $args['after_title'];
        } ?>
            <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
        <?php
        echo $args['after_widget'];
    }

    /**
     * @param array $new_instance
     * @param array $old_instance
     * @return array
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        if ( current_user_can('unfiltered_html') )
            $instance['text'] =  $new_instance['text'];
        else
            $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
        $instance['filter'] = ! empty( $new_instance['filter'] );
        return $instance;
    }

    /**
     * @param array $instance
     */
    public function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
        $title = strip_tags($instance['title']);
        $text = esc_textarea($instance['text']);
?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>

        <p><label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label>
        <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea></p>

        <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
    }
}
?>
_0 tetapi jangan ragu untuk memilih nama yang Anda inginkan. Karena konstruktor kelas ini menetapkan beberapa informasi tentang widget itu sendiri, kita juga perlu memodifikasinya

<?php
public function __construct() {
    $widget_ops = array('classname' => 'widget_shortcodes', 'description' => __('Arbitrary text or HTML with shortcodes.'));
    $control_ops = array('width' => 400, 'height' => 350);
    parent::__construct('shortcodes', __('Shortcodes'), $widget_ops, $control_ops);
}
?>

Hal lain yang perlu diubah adalah metode

<?php
class WP_Widget_Text extends WP_Widget {

    public function __construct() {
        $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML.'));
        $control_ops = array('width' => 400, 'height' => 350);
        parent::__construct('text', __('Text'), $widget_ops, $control_ops);
    }

    /**
     * @param array $args
     * @param array $instance
     */
    public function widget( $args, $instance ) {
        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        /**
         * Filter the content of the Text widget.
         *
         * @since 2.3.0
         *
         * @param string    $widget_text The widget content.
         * @param WP_Widget $instance    WP_Widget instance.
         */
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
        echo $args['before_widget'];
        if ( ! empty( $title ) ) {
            echo $args['before_title'] . $title . $args['after_title'];
        } ?>
            <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
        <?php
        echo $args['after_widget'];
    }

    /**
     * @param array $new_instance
     * @param array $old_instance
     * @return array
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        if ( current_user_can('unfiltered_html') )
            $instance['text'] =  $new_instance['text'];
        else
            $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
        $instance['filter'] = ! empty( $new_instance['filter'] );
        return $instance;
    }

    /**
     * @param array $instance
     */
    public function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
        $title = strip_tags($instance['title']);
        $text = esc_textarea($instance['text']);
?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>

        <p><label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label>
        <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea></p>

        <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
    }
}
?>
1 yang menjelaskan ke WordPress cara menampilkan widget. Kami mengubah konten variabel
<?php
class WP_Widget_Text extends WP_Widget {

    public function __construct() {
        $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML.'));
        $control_ops = array('width' => 400, 'height' => 350);
        parent::__construct('text', __('Text'), $widget_ops, $control_ops);
    }

    /**
     * @param array $args
     * @param array $instance
     */
    public function widget( $args, $instance ) {
        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        /**
         * Filter the content of the Text widget.
         *
         * @since 2.3.0
         *
         * @param string    $widget_text The widget content.
         * @param WP_Widget $instance    WP_Widget instance.
         */
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
        echo $args['before_widget'];
        if ( ! empty( $title ) ) {
            echo $args['before_title'] . $title . $args['after_title'];
        } ?>
            <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
        <?php
        echo $args['after_widget'];
    }

    /**
     * @param array $new_instance
     * @param array $old_instance
     * @return array
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        if ( current_user_can('unfiltered_html') )
            $instance['text'] =  $new_instance['text'];
        else
            $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
        $instance['filter'] = ! empty( $new_instance['filter'] );
        return $instance;
    }

    /**
     * @param array $instance
     */
    public function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
        $title = strip_tags($instance['title']);
        $text = esc_textarea($instance['text']);
?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>

        <p><label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label>
        <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea></p>

        <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
    }
}
?>
2 yang berisi teks untuk ditampilkan. Kami menghapus panggilan ke filter widget_text_, dan kami menerapkan fungsi do_shortcode() ke konten ini

<?php
$text = empty( $instance['text'] ) ? '' : do_shortcode($instance['text']);
?>
_

Kami kemudian perlu mendaftarkan widget kami, untuk dapat menambahkannya seperti widget lainnya. Ini dapat dicapai berkat tindakan

<?php
class WP_Widget_Text extends WP_Widget {

    public function __construct() {
        $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML.'));
        $control_ops = array('width' => 400, 'height' => 350);
        parent::__construct('text', __('Text'), $widget_ops, $control_ops);
    }

    /**
     * @param array $args
     * @param array $instance
     */
    public function widget( $args, $instance ) {
        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        /**
         * Filter the content of the Text widget.
         *
         * @since 2.3.0
         *
         * @param string    $widget_text The widget content.
         * @param WP_Widget $instance    WP_Widget instance.
         */
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
        echo $args['before_widget'];
        if ( ! empty( $title ) ) {
            echo $args['before_title'] . $title . $args['after_title'];
        } ?>
            <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
        <?php
        echo $args['after_widget'];
    }

    /**
     * @param array $new_instance
     * @param array $old_instance
     * @return array
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        if ( current_user_can('unfiltered_html') )
            $instance['text'] =  $new_instance['text'];
        else
            $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
        $instance['filter'] = ! empty( $new_instance['filter'] );
        return $instance;
    }

    /**
     * @param array $instance
     */
    public function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
        $title = strip_tags($instance['title']);
        $text = esc_textarea($instance['text']);
?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>

        <p><label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label>
        <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea></p>

        <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
    }
}
?>
_5 yang dipicu setelah WordPress mendaftarkan widget default

<?php
add_action('widgets_init', function() {
    register_widget('WP_Widget_Shortcodes');
});
?>
_

Sekarang Anda dapat menemukan widget kami di daftar widget yang tersedia. Anda dapat menambahkannya ke zona mana pun yang kompatibel, dan itu akan menginterpretasikan kode pendek apa pun yang Anda gunakan

Bagaimana Anda memanggil kode pendek di widget di wordpress?

Kata Penutup

Seperti yang kita lihat di atas, mengaktifkan penggunaan shortcode di widget tidak terlalu rumit, tetapi Anda harus berhati-hati. Faktanya, tidak setiap kode pendek cocok dengan tempat widget ditampilkan. Jika shortcode memiliki lebar tetap, itu bisa menjadi masalah

Perhatikan bahwa perubahan yang kami gunakan di widget 'Teks' default minimal. Anda dapat mengubah apa pun jika ingin menyesuaikan widget Anda. Anda dapat mengambil filter yang kami gunakan dan widget yang kami buat dalam plugin contoh yang tersedia untuk diunduh di sini jika Anda ingin bereksperimen lebih lanjut

Bagikan Artikel Ini

Jeremy Helene

Saat ini seorang mahasiswa matematika, Jérémy adalah seorang pria yang bersemangat yang tertarik pada banyak bidang, terutama di dunia teknologi tinggi yang meliput berita setiap hari di beberapa blog, dan pengembangan web yang menghabiskan banyak waktu luangnya. Dia suka mempelajari hal-hal baru dan berbagi pengetahuannya dengan orang lain

Bagaimana Anda memanggil kode pendek dari menu di WordPress?

Periksa opsi layar, jika Anda tidak melihat kotak Shortcode. Centang opsi Shortcode untuk melihat kotak Shortcode baru. Tambahkan kode pendek/HTML Anda ke area teks (bukan tautan, di tangkapan layar). Secara opsional, tambahkan judul.

Bagaimana cara menambahkan kode pendek ke widget HTML khusus di WordPress?

Untuk menambahkan shortcode menggunakan widget Custom HTML, navigasi ke Appearance > Widgets dan temukan area widget tempat Anda ingin menyisipkan shortcode. Tambahkan widget HTML Khusus di dalam area widget pilihan Anda, lalu tambahkan panggilan shortcode ke dalamnya . Selesaikan dengan mengklik tombol Simpan di bawah.