向父主题中的方法添加筛选器

时间:2016-11-21 作者:H Aßdøµ

我有一个带有此筛选器的子主题:

function translate_subscribe_btn() {
    $new_form = str_replace(\'Subscribe\', \'إشترك الآن\', $form);
    return $new_form;
}

add_filter(array(\'Raymond_NewsletterWidget\', \'get_widget_form\'), \'translate_subscribe_btn\');
原始方法:类Raymond\\u NewsletterWidget扩展了WP\\u Widget{

function __construct() {

    parent::__construct(false, $name = \'@Raymond Newsletter\', array(\'description\' => \'Newsletter widget to add subscription forms on sidebars\'), array(\'width\' => \'350px\'));

}



static function get_widget_form( array $instance=array() ) {

    $options_profile = get_option(\'newsletter_profile\');

    $placeholder =( isset( $instance[\'placeholder\'] ) ) ? $instance[\'placeholder\'] : __(\'Email\',\'raymond\');

    $email = is_email( $options_profile[\'email\'] ) ? $options_profile[\'email\'] : $placeholder;

    $form = NewsletterSubscription::instance()->get_form_javascript();



    $form .= \'<form action="\' . home_url(\'/\') . \'?na=s" onsubmit="return newsletter_check(this)" method="post">\';

    // Referrer

    $form .= \'<input type="hidden" name="nr" value="widget"/>\';



    if ($options_profile[\'name_status\'] == 2)

        $form .= \'<p><input class="newsletter-firstname" placeholder="\'. esc_attr( $placeholder ) .\'" type="text" name="nn" value="\' . esc_attr($options_profile[\'name\']) . \'" onclick="if (this.defaultValue==this.value) this.value=\\\'\\\'" onblur="if (this.value==\\\'\\\') this.value=this.defaultValue"/></p>\';



    if ($options_profile[\'surname_status\'] == 2)

        $form .= \'<p><input class="newsletter-lastname" placeholder="\'. esc_attr( $placeholder ) .\'" type="text" name="ns" value="\' . esc_attr($options_profile[\'surname\']) . \'" onclick="if (this.defaultValue==this.value) this.value=\\\'\\\'" onblur="if (this.value==\\\'\\\') this.value=this.defaultValue"/></p>\';



    $form .= \'<p><input class="newsletter-email" type="email" placeholder="\'. esc_attr( $placeholder ) .\'" required name="ne" value="\' . esc_attr( $email ) . \'" onclick="if (this.defaultValue==this.value) this.value=\\\'\\\'" onblur="if (this.value==\\\'\\\') this.value=this.defaultValue"/></p>\';



    if (isset($options_profile[\'sex_status\']) && $options_profile[\'sex_status\'] == 2) {

        $form .= \'<p><select name="nx" class="newsletter-sex">\';

        $form .= \'<option value="m">\' . $options_profile[\'sex_male\'] . \'</option>\';

        $form .= \'<option value="f">\' . $options_profile[\'sex_female\'] . \'</option>\';

        $form .= \'</select></p>\';

    }



    // Extra profile fields

    for ($i = 1; $i <= NEWSLETTER_PROFILE_MAX; $i++) {

        if ($options_profile[\'profile_\' . $i . \'_status\'] != 2)

            continue;

        if ($options_profile[\'profile_\' . $i . \'_type\'] == \'text\') {

            $form .= \'<p><input class="newsletter-profile newsletter-profile-\' . $i . \'" type="text" name="np\' . $i . \'" value="\' . $options_profile[\'profile_\' . $i] . \'" onclick="if (this.defaultValue==this.value) this.value=\\\'\\\'" onblur="if (this.value==\\\'\\\') this.value=this.defaultValue"/></p>\';

        }

        if ($options_profile[\'profile_\' . $i . \'_type\'] == \'select\') {

            $form .= \'<p>\' . $options_profile[\'profile_\' . $i] . \'<br /><select class="newsletter-profile newsletter-profile-\' . $i . \'" name="np\' . $i . \'">\';

            $opts = explode(\',\', $options_profile[\'profile_\' . $i . \'_options\']);

            for ($t = 0; $t < count($opts); $t++) {

                $form .= \'<option>\' . trim($opts[$t]) . \'</option>\';

            }

            $form .= \'</select></p>\';

        }

    }



    $lists = \'\';

    for ($i = 1; $i <= NEWSLETTER_LIST_MAX; $i++) {

        if ($options_profile[\'list_\' . $i . \'_status\'] != 2)

            continue;

        $lists .= \'<input type="checkbox" name="nl[]" value="\' . $i . \'"\';

        if ($options_profile[\'list_\' . $i . \'_checked\'] == 1)

            $lists .= \' checked\';

        $lists .= \'/>&nbsp;\' . $options_profile[\'list_\' . $i] . \'<br />\';

    }

    if (!empty($lists))

        $form .= \'<p>\' . $lists . \'</p>\';





    $extra = apply_filters(\'newsletter_subscription_extra\', array());

    foreach ($extra as &$x) {

        $form .= "<p>";

        if (!empty($x[\'label\']))

            $form .= $x[\'label\'] . "<br/>";

        $form .= $x[\'field\'] . "</p>";

    }



    if ($options_profile[\'privacy_status\'] == 1) {

        if (!empty($options_profile[\'privacy_url\'])) {

            $form .= \'<p><input type="checkbox" name="ny"/>&nbsp;<a target="_blank" href="\' . $options_profile[\'privacy_url\'] . \'">\' . $options_profile[\'privacy\'] . \'</a></p>\';

        }

        else

            $form .= \'<p><input type="checkbox" name="ny"/>&nbsp;\' . $options_profile[\'privacy\'] . \'</p>\';

    }



    if (strpos($options_profile[\'subscribe\'], \'http://\') !== false) {

        $form .= \'<p><input class="newsletter-submit" type="image" src="\' . $options_profile[\'subscribe\'] . \'"/></p>\';

    } else {

        $subscribe_text = \'Subscribe\';
        /*if ( is_rtl() ) {
            $subscribe_text = \'إشترك الآن\';
        }*/
        //$form .= \'<p><input class="newsletter-submit" type="submit" value="\' . $options_profile[\'subscribe\'] . \'"/></p>\';
        $form .= \'<p><input class="newsletter-submit" type="submit" value="\' . $subscribe_text . \'"/></p>\';

    }

    // hide popup

    $form .= \'<div class="tb-newsletter-checkbox"><input type="checkbox" name="hide_popup" id="tb-hide-popup"><span>\' . esc_html__("Don’t show this popup again","raymond") .\'</span></div>\';



    $form .= \'</form>\';



    return $form;

}
}Theget_widget_form 是一种静态方法。现在,当我刷新主页时,按钮仍然具有相同的值\'Subscribe\', 我的问题是,为什么过滤器没有更改值?

1 个回复
SO网友:Mladjo

Try with init hook

add_action(\'init\', \'child_theme_hooks\');

function child_theme_hooks() {
    add_filter(array(\'Raymond_NewsletterWidget\', \'get_widget_form\'), \'translate_subscribe_btn\');
}

function translate_subscribe_btn() {
    $new_form = str_replace(\'Subscribe\', \'إشترك الآن\', $form);
    return $new_form;
}

相关推荐

Can I make plugins required?

我正在开发自己的Wordpress主题,将用于多个客户端。它有一个主题选项页面,这样我每次都可以轻松自定义网站,但我也会在我制作的每个网站上使用一些插件(如SEO插件、安全性等)。有没有办法让它们成为“必需的”,这样我就可以得到这些插件的列表,这样当我在新网站上安装主题时就不必在插件目录中找到它们了?