我怎样才能编写wordpress插件的简短代码

时间:2013-03-07 15:55:29

标签: wordpress plugins shortcode

这是各国插件中的代码,但我不知道如何以简短的形式编写此代码,

  function save_details( $post_id ) {
            global $post;

            $post_vars = shortcode_atts( array(
                'country_code'     => '',
                'country_list'     => '',
                'flags'            => '',
                'country_details'  => '',
                'country_currency' => '',
                'currency_symbol'  => '',
                'currency_html'    => '',
                'currency_code'    => '',
                'city_list'        => ''
            ), $_POST );

有人能告诉我我应该写一些简短的代码,以便在一个页面上获取所有国家的名字吗?

1 个答案:

答案 0 :(得分:0)

您可以像这样重写您的功能

add_shortcode('save_details', function( $atts ) {
        global $post;

        $post_vars = array(
            'country_code'     => '',
            'country_list'     => '',
            'flags'            => '',
            'country_details'  => '',
            'country_currency' => '',
            'currency_symbol'  => '',
            'currency_html'    => '',
            'currency_code'    => '',
            'city_list'        => ''
        );
        extract( shortcode_atts( $post_vars, $atts ) );
        // country_code is now available like so $country_code
        // return data after your done

 });

并像这样访问

//all of your attributes can be used inside the sq brackets
[save_details] and [save_details country_code = "" country_list = ""]