最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Modify function output in a plugin

programmeradmin1浏览0评论

I am trying to update one my plugin functions using add_filter with the ultimate goal of changing the order of elements that are output by the plugin.

Here is the plugin function:

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;
}

and the add_filter I've written for the functions.php:

function modify_store_output() {
$content = $contact . $address . $filter . $map . $content;
return $content;
}
add_filter ('stores_single_content', 'modify_store_output', 15);

Trying to understand why that's not working :(

I am trying to update one my plugin functions using add_filter with the ultimate goal of changing the order of elements that are output by the plugin.

Here is the plugin function:

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;
}

and the add_filter I've written for the functions.php:

function modify_store_output() {
$content = $contact . $address . $filter . $map . $content;
return $content;
}
add_filter ('stores_single_content', 'modify_store_output', 15);

Trying to understand why that's not working :(

Share Improve this question asked Jul 25, 2019 at 17:41 lushiris02lushiris02 357 bronze badges 2
  • Can you try changing the first line of your filter callback function to function modify_store_output($content) { – rudtek Commented Jul 25, 2019 at 18:29
  • 2 Can you show us the call to apply_filters('stores_single_content', ...)? I don't see it in your modified plugin code. In particular, what does it pass as arguments to the filter? You won't have contact, address, filter, map and content in-scope in your modify_store_output automatically: you'll only have them if they're passed by the filter call, or if you can unpack them from an object passed by the filter call. – Rup Commented Jul 25, 2019 at 19:06
Add a comment  | 

1 Answer 1

Reset to default 0

You haven't created your filter yet. You can't just filter a function.

You'll need to call the function apply_filters(), and then you can use it in your functions.php file.

So above return $content; add something like this:

$content = apply_filters( 'my_content_filter_name', $new_content, $content );

Then, use it like this:

function modify_store_output( $content ) {
    $content = $contact . $address . $filter . $map . $content;
    return $content;
}
add_filter ('my_content_filter_name', 'modify_store_output', 15, 1);

Notice how $content is passing through the filter to the input of your modify_store_output function. Also, the 1 at the end of the filter is how many of these variables (arguments) you are passing through. In this case it is only one, but if you wanted to pass more arguments to your filter function you would increase the number to however many you are using in your add_filter() callback function.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论