修改插件中的函数输出

时间:2019-07-25 作者:lushiris02

我正在尝试使用add\\u filter更新我的一个插件函数,最终目标是更改插件输出的元素的顺序。

以下是插件功能:

public function stores_single_content($content) 
{
    global $post;

    if(!isset($post->post_type)) {
        return $content;
    }

    if ($post->post_type == \'stores\') {

        $prefix = "wordpress_store_locator_";
        $meta = get_post_meta($post->ID);

        $args = array(\'fields\' => \'names\', \'orderby\' => \'name\', \'order\' => \'ASC\');

        // Categories
        $store_cats = wp_get_object_terms($post->ID, \'store_category\', $args);
        $categories = \'<div class="store_locator_single_categories">\';
            $categories .= \'<strong class="store_locator_single_categories_title">\' . __(\'Categories: \', \'wordpress-store-locator\') . \'</strong>\' . implode(\', \', $store_cats);
        $categories .= \'</div>\';

        $store_filter = wp_get_object_terms($post->ID, \'store_filter\', $args);
        $filter = \'<div class="store_locator_single_filter">\';
            $filter .= \'<strong class="store_locator_single_filter_title">\' . __(\'Filter: \', \'wordpress-store-locator\') . \'</strong>\' . implode(\', \', $store_filter);
        $filter .= \'</div>\';

        $address1 = isset($meta[ $prefix . \'address1\' ][0]) ? $meta[ $prefix . \'address1\' ][0] : \'\';
        $address2 = isset($meta[ $prefix . \'address2\' ][0]) ? $meta[ $prefix . \'address2\' ][0] : \'\';
        $zip = isset($meta[ $prefix . \'zip\' ][0]) ? $meta[ $prefix . \'zip\' ][0] : \'\';
        $city = isset($meta[ $prefix . \'city\' ][0]) ? $meta[ $prefix . \'city\' ][0] : \'\';
        $region = isset($meta[ $prefix . \'region\' ][0]) ? $meta[ $prefix . \'region\' ][0] : \'\';
        $country = isset($meta[ $prefix . \'country\' ][0]) ? $meta[ $prefix . \'country\' ][0] : \'\';
        $telephone = isset($meta[ $prefix . \'telephone\' ][0]) ? $meta[ $prefix . \'telephone\' ][0] : \'\';
        $mobile = isset($meta[ $prefix . \'mobile\' ][0]) ? $meta[ $prefix . \'mobile\' ][0] : \'\';
        $fax = isset($meta[ $prefix . \'fax\' ][0]) ? $meta[ $prefix . \'fax\' ][0] : \'\';
        $email = isset($meta[ $prefix . \'email\' ][0]) ? $meta[ $prefix . \'email\' ][0] : \'\';
        $website = isset($meta[ $prefix . \'website\' ][0]) ? $meta[ $prefix . \'website\' ][0] : \'\';

        $description = "";
        if($this->get_option(\'showAddressStyle\') == "american") {
            $address = \'<div class="store_locator_single_address">\';
                $address .=  \'<h2>\' . __(\'Address \', \'wordpress-store-locator\') . \'</h2>\';
                $address .= !empty($address1) ? $address1 . \'<br/>\' : \'\';
                $address .= !empty($address2) ? $address2 . \'<br/>\' : \'\';
                $address .= !empty($city) ? $city . \', \' : \'\';
                $address .= !empty($region) ? $region . \' \' : \'\';
                $address .= !empty($zip) ? $zip . \'<br/>\' : \'\';
                if($this->get_option(\'showCountry\')) {
                    $address .= !empty($country) ? $country : \'\';
                }
            $address .= \'</div>\';
        } else {
            $address = \'<div class="store_locator_single_address">\';
                $address .=  \'<h2>\' . __(\'Address \', \'wordpress-store-locator\') . \'</h2>\';
                $address .= !empty($address1) ? $address1 . \'<br/>\' : \'\';
                $address .= !empty($address2) ? $address2 . \'<br/>\' : \'\';
                $address .= !empty($zip) ? $zip . \', \' : \'\';
                $address .= !empty($city) ? $city . \', \' : \'\';
                $address .= !empty($region) ? $region . \', \' : \'\';
                if($this->get_option(\'showCountry\')) {
                    $address .= !empty($country) ? $country : \'\';
                }
            $address .= \'</div>\';
        }

        $contact = \'<div class="store_locator_single_contact">\';
            $contact .=  \'<h2>\' . __(\'Contact \', \'wordpress-store-locator\') . \'</h2>\';
            $contact .= !empty($telephone) && $this->get_option(\'showTelephone\') ? 
                        $this->get_option(\'showTelephoneText\') . \': <a href="tel:\' .  $telephone  . \'">\' . $telephone . \'</a><br/>\' : \'\';
            $contact .= !empty($mobile) && $this->get_option(\'showMobile\') ? 
                        $this->get_option(\'showMobileText\') . \': <a href="tel:\' .  $mobile  . \'">\' . $mobile . \'</a><br/>\' : \'\';
            $contact .= !empty($fax) && $this->get_option(\'showFax\') ? 
                        $this->get_option(\'showFaxText\') . \': <a href="tel:\' .  $fax  . \'">\' . $fax . \'</a><br/>\' : \'\';
            $contact .= !empty($email) && $this->get_option(\'showEmail\') ? 
                        $this->get_option(\'showEmailText\') . \': <a href="mailto:\' .  $email  . \'">\' . $email . \'</a><br/>\' : \'\';
            $contact .= !empty($website) && $this->get_option(\'showWebsite\') ? 
                        $this->get_option(\'showWebsiteText\') . \': <a href="\' .  $website  . \'" target="_blank">\' . $website . \'</a><br/>\' : \'\';
        $contact .= \'</div>
                    <div class="store_locator_single_clear"></div>\';

        $map = "";
        $opening_hours = "";
        $opening_hours2 = "";
        $contactStore = "";
        if(is_single()) {

            $weekdays = array(
                \'Monday\' => __(\'Monday\', \'wordpress-store-locator\'),
                \'Tuesday\' => __(\'Tuesday\', \'wordpress-store-locator\'),
                \'Wednesday\' => __(\'Wednesday\', \'wordpress-store-locator\'),
                \'Thursday\' => __(\'Thursday\', \'wordpress-store-locator\'),
                \'Friday\' => __(\'Friday\', \'wordpress-store-locator\'),
                \'Saturday\' => __(\'Saturday\', \'wordpress-store-locator\'),
                \'Sunday\' => __(\'Sunday\', \'wordpress-store-locator\'),
            );

            foreach ($weekdays as $key => $weekday) {
                $open = isset($meta[ $prefix . $key . "_open"]) ? $meta[ $prefix . $key . "_open"][0] : \'\';
                $close = isset($meta[ $prefix . $key . "_close"]) ? $meta[ $prefix . $key . "_close"][0] : \'\';

                if(!empty($open) && !empty($close)) {
                    $opening_hours .= $weekday . \': \' . $open . \' – \' . $close . \' \' . $this->get_option(\'showOpeningHoursClock\') . \'<br/>\';
                } elseif(!empty($open)) {
                    $opening_hours .= $weekday . \': \' . $open . \' \' . $this->get_option(\'showOpeningHoursClock\') . \'<br/>\';
                } elseif(!empty($close)) {
                    $opening_hours .= $weekday . \': \' . $close . \' \' . $this->get_option(\'showOpeningHoursClock\') . \'<br/>\';
                }
            }
            if(!empty($opening_hours)) {
                $opening_hours = \'<div class="store_locator_single_opening_hours">\' . 
                                    \'<h2>\' . __(\'Opening Hours \', \'wordpress-store-locator\') . \'</h2>\' .
                                    $opening_hours . 
                                \'</div>\';
            }

            foreach ($weekdays as $key => $weekday) {
                $open = isset($meta[ $prefix . $key . "_open2"]) ? $meta[ $prefix . $key . "_open2"][0] : \'\';
                $close = isset($meta[ $prefix . $key . "_close2"]) ? $meta[ $prefix . $key . "_close2"][0] : \'\';

                if(!empty($open) && !empty($close)) {
                    $opening_hours2 .= $weekday . \': \' . $open . \' – \' . $close . \' \' . $this->get_option(\'showOpeningHours2Clock\') . \'<br/>\';
                } elseif(!empty($open)) {
                    $opening_hours2 .= $weekday . \': \' . $open . \' \' . $this->get_option(\'showOpeningHours2Clock\') . \'<br/>\';
                } elseif(!empty($close)) {
                    $opening_hours2 .= $weekday . \': \' . $close . \' \' . $this->get_option(\'showOpeningHours2Clock\') . \'<br/>\';
                }
            }
            if(!empty($opening_hours2)) {
                $opening_hours2 = \'<div class="store_locator_single_opening_hours2">\' . 
                                    \'<h2>\' . $this->get_option(\'showOpeningHours2Text\') . \'</h2>\' .
                                    $opening_hours2 . 
                                \'</div>\';
            }

            if($this->get_option(\'showContactStore\')) {
                $contactStorePage = $this->get_option(\'showContactStorePage\');
                $contactStoreText = $this->get_option(\'showContactStoreText\');
                if(!empty($contactStorePage)) {
                    $contactStorePage = get_permalink($contactStorePage) . \'?store_id=\' . $post->ID;
                }
                $contactStore = \'<div class="store_locator_single_contact_store">\' . 
                                    \'<a href="\' . $contactStorePage . \'" class="store_locator_contact_store_button btn button et_pb_button btn-primary theme-button btn-lg center">\' . $contactStoreText . \'</a>\'. 
                                \'</div>\';
            }

            $map .= \'<div id="store_locator_single_map" class="store_locator_single_map" 
                                data-lat="\' . $meta[ $prefix . \'lat\' ][0] . \'" 
                                data-lng="\' . $meta[ $prefix . \'lng\' ][0] . \'"></div>\';
        }

        $content = $categories . $filter . $content . $address . $contact . $opening_hours . $opening_hours2 . $contactStore . $map;
    }

    return $content;
}
还有我为函数编写的add\\u过滤器。php:

function modify_store_output() {
$content = $contact . $address . $filter . $map . $content;
return $content;
}
add_filter (\'stores_single_content\', \'modify_store_output\', 15);
试图理解为什么这不起作用:(

1 个回复
SO网友:Justin Waulters

您尚未创建筛选器。你不能只过滤一个函数。

您需要调用函数apply_filters(), 然后你可以在你的functions.php 文件

如上所述return $content; 添加如下内容:

$content = apply_filters( \'my_content_filter_name\', $new_content, $content );
然后,像这样使用它:

function modify_store_output( $content ) {
    $content = $contact . $address . $filter . $map . $content;
    return $content;
}
add_filter (\'my_content_filter_name\', \'modify_store_output\', 15, 1);
注意如何$content 正在通过过滤器输入modify_store_output 作用此外1 过滤器的末尾是您正在传递的这些变量(参数)的数量。在本例中,它只有一个,但是如果您想向过滤器函数传递更多参数,那么您可以将该数量增加到在add_filter() 回调函数。

相关推荐

static array on functions.php

在函数中。php中,我想访问数据库一次,以获取一个ACF对象,然后在本地“保存”它,以防止对数据库的进一步请求。我想首先在“init”钩子中调用以下函数。然后,假设,当我在以后的挂钩上调用它时,由于使用了“static”关键字,$current\\u store var已经设置好了,函数将在第一个“if”返回已经保存的静态var时停止。它不起作用-当访问稍后挂钩上的函数时,“isset($current\\u store)”返回false。我做错了什么?function get_current_store