¿Es posible usar add_filter desde un tema para alterar una función de un plugin?

15 ene 2011, 22:45:58
Vistas: 271
Votos: 1

¿Se puede usar add_filter() para interceptar una función de un plugin?

No estoy teniendo éxito, así que creo que quizás lo esté haciendo mal.

El plugin es "All in One SEO" y la función que intento interceptar está en la clase All_in_One_SEO_Pack y se llama get_original_title()

Aquí está el código que intento interceptar... específicamente, cuando se llama al método is_404()...

class All_in_One_SEO_Pack {

function rewrite_title($header) {
    global $aioseop_options;
    global $wp_query;
    if (!$wp_query) {
        $header .= "<!-- ¡No se encontró wp_query! -->\n";
        return $header; 
    }

    $post = $wp_query->get_queried_object();

    // the_search_query() no es adecuado, no puede simplemente retornar
    global $s;

    global $STagging;

    if (is_home() && !$this->is_static_posts_page()) {
        $title = $this->internationalize($aioseop_options['aiosp_home_title']);
        if (empty($title)) {
            $title = $this->internationalize(get_option('blogname'));
        }
        $title = $this->paged_title($title);
        $header = $this->replace_title($header, $title);
    } else if (is_attachment()) { 
                        $title = get_the_title($post->post_parent).' '.$post->post_title.' – '.get_option('blogname');
                        $header = $this->replace_title($header,$title);
    } else if (is_single()) {
        // no estamos en el loop :(
        $authordata = get_userdata($post->post_author);
        $categories = get_the_category();
        $category = '';
        if (count($categories) > 0) {
            $category = $categories[0]->cat_name;
        }
        $title = $this->internationalize(get_post_meta($post->ID, "_aioseop_title", true));
        if (!$title) {
            $title = $this->internationalize(get_post_meta($post->ID, "title_tag", true));
            if (!$title) {
                $title = $this->internationalize(wp_title('', false));
            }
        }
        $title_format = $aioseop_options['aiosp_post_title_format'];
        /*
        $new_title = str_replace('%blog_title%', $this->internationalize(get_bloginfo('name')), $title_format);
        $new_title = str_replace('%blog_description%', $this->internationalize(get_bloginfo('description')), $new_title);
        $new_title = str_replace('%post_title%', $title, $new_title);
        $new_title = str_replace('%category%', $category, $new_title);
        $new_title = str_replace('%category_title%', $category, $new_title);
        $new_title = str_replace('%post_author_login%', $authordata->user_login, $new_title);
        $new_title = str_replace('%post_author_nicename%', $authordata->user_nicename, $new_title);
        $new_title = str_replace('%post_author_firstname%', ucwords($authordata->first_name), $new_title);
        $new_title = str_replace('%post_author_lastname%', ucwords($authordata->last_name), $new_title);
        */
        $r_title = array('%blog_title%','%blog_description%','%post_title%','%category%','%category_title%','%post_author_login%','%post_author_nicename%','%post_author_firstname%','%post_author_lastname%');
        $d_title = array($this->internationalize(get_bloginfo('name')),$this->internationalize(get_bloginfo('description')),$title, $category, $category, $authordata->user_login, $authordata->user_nicename, ucwords($authordata->first_name), ucwords($authordata->last_name));
        $title = trim(str_replace($r_title, $d_title, $title_format));

//      $title = $new_title;
//      $title = trim($title);
        $title = apply_filters('aioseop_title_single',$title);
        $header = $this->replace_title($header, $title);
    } else if (is_search() && isset($s) && !empty($s)) {
        if (function_exists('attribute_escape')) {
            $search = attribute_escape(stripcslashes($s));
        } else {
            $search = wp_specialchars(stripcslashes($s), true);
        }
        $search = $this->capitalize($search);
        $title_format = $aioseop_options['aiosp_search_title_format'];
        $title = str_replace('%blog_title%', $this->internationalize(get_bloginfo('name')), $title_format);
        $title = str_replace('%blog_description%', $this->internationalize(get_bloginfo('description')), $title);
        $title = str_replace('%search%', $search, $title);
        $header = $this->replace_title($header, $title);
    } else if (is_category() && !is_feed()) {
        $category_description = $this->internationalize(category_description());
            if($aioseop_options['aiosp_cap_cats']){
                $category_name = ucwords($this->internationalize(single_cat_title('', false)));
            }else{
                    $category_name = $this->internationalize(single_cat_title('', false));
            }           
        //$category_name = ucwords($this->internationalize(single_cat_title('', false)));
        $title_format = $aioseop_options['aiosp_category_title_format'];
        $title = str_replace('%category_title%', $category_name, $title_format);
        $title = str_replace('%category_description%', $category_description, $title);
        $title = str_replace('%blog_title%', $this->internationalize(get_bloginfo('name')), $title);
        $title = str_replace('%blog_description%', $this->internationalize(get_bloginfo('description')), $title);
        $title = $this->paged_title($title);
        $header = $this->replace_title($header, $title);
    } else if (is_page() || $this->is_static_posts_page()) {
        // no estamos en el loop :(
        $authordata = get_userdata($post->post_author);
        if ($this->is_static_front_page()) {
            if ($this->internationalize($aioseop_options['aiosp_home_title'])) {

                        //filtro de título de inicio
                        $home_title = $this->internationalize($aioseop_options['aiosp_home_title']);
                        $home_title = apply_filters('aioseop_home_page_title',$home_title);
                        $header = $this->replace_title($header, $home_title);

            }
        } else {
            $title = $this->internationalize(get_post_meta($post->ID, "_aioseop_title", true));
            if (!$title) {
                $title = $this->internationalize(wp_title('', false));
            }
            $title_format = $aioseop_options['aiosp_page_title_format'];
            $new_title = str_replace('%blog_title%', $this->internationalize(get_bloginfo('name')), $title_format);
            $new_title = str_replace('%blog_description%', $this->internationalize(get_bloginfo('description')), $new_title);
            $new_title = str_replace('%page_title%', $title, $new_title);
            $new_title = str_replace('%page_author_login%', $authordata->user_login, $new_title);
            $new_title = str_replace('%page_author_nicename%', $authordata->user_nicename, $new_title);
            $new_title = str_replace('%page_author_firstname%', ucwords($authordata->first_name), $new_title);
            $new_title = str_replace('%page_author_lastname%', ucwords($authordata->last_name), $new_title);
            $title = trim($new_title);
                            $title = $this->paged_title($title);
            $title = apply_filters('aioseop_title_page',$title);
            $header = $this->replace_title($header, $title);
        }
    } else if (function_exists('is_tag') && is_tag()) {
        global $utw;
        if ($utw) {
            $tags = $utw->GetCurrentTagSet();
            $tag = $tags[0]->tag;
            $tag = str_replace('-', ' ', $tag);
        } else {
            // wordpress > 2.3
            $tag = $this->internationalize(wp_title('', false));
        }
        if ($tag) {
            $tag = $this->capitalize($tag);
            $title_format = $aioseop_options['aiosp_tag_title_format'];
            $title = str_replace('%blog_title%', $this->internationalize(get_bloginfo('name')), $title_format);
            $title = str_replace('%blog_description%', $this->internationalize(get_bloginfo('description')), $title);
            $title = str_replace('%tag%', $tag, $title);
            $title = $this->paged_title($title);
            $header = $this->replace_title($header, $title);
        }
    } else if (isset($STagging) && $STagging->is_tag_view()) { // soporte para simple tagging
        $tag = $STagging->search_tag;
        if ($tag) {
            $tag = $this->capitalize($tag);
            $title_format = $aioseop_options['aiosp_tag_title_format'];
            $title = str_replace('%blog_title%', $this->internationalize(get_bloginfo('name')), $title_format);
            $title = str_replace('%blog_description%', $this->internationalize(get_bloginfo('description')), $title);
            $title = str_replace('%tag%', $tag, $title);
            $title = $this->paged_title($title);
            $header = $this->replace_title($header, $title);
        }
    } else if (is_archive()) {
        $date = $this->internationalize(wp_title('', false));
        $title_format = $aioseop_options['aiosp_archive_title_format'];
        $new_title = str_replace('%blog_title%', $this->internationalize(get_bloginfo('name')), $title_format);
        $new_title = str_replace('%blog_description%', $this->internationalize(get_bloginfo('description')), $new_title);
        $new_title = str_replace('%date%', $date, $new_title);
        $title = trim($new_title);
        $title = $this->paged_title($title);
        $header = $this->replace_title($header, $title);
    } else if (is_404()) {
        $title_format = $aioseop_options['aiosp_404_title_format'];
        $new_title = str_replace('%blog_title%', $this->internationalize(get_bloginfo('name')), $title_format);
        $new_title = str_replace('%blog_description%', $this->internationalize(get_bloginfo('description')), $new_title);
        $new_title = str_replace('%request_url%', $_SERVER['REQUEST_URI'], $new_title);
        $new_title = str_replace('%request_words%', $this->request_as_words($_SERVER['REQUEST_URI']), $new_title);
        $new_title = str_replace('%404_title%', $this->internationalize(wp_title('', false)), $new_title);
        $header = $this->replace_title($header, $new_title);
    }

    return $header;

}}
0
Todas las respuestas a la pregunta 2
2

Lo es, pero lo estás haciendo mal. No estoy familiarizado con ese plugin, pero es probable que esté enganchando al hook wp_title. Deberías poder a) llamar a remove_filter() desde tu tema para desactivar la manipulación del título por parte del plugin, y/o b) anular el resultado devuelto por el plugin enganchándote al mismo hook (solo asegúrate de que tu "priority" sea "más alta", es decir, que tu función se llame después).

16 ene 2011 00:22:02
Comentarios

¿Puedo apuntar específicamente a la función is_404() y eliminarla o modificarla? add_filter('is_404', 'myfunction', 1,0) obviamente no funciona, pero ¿es posible con el filtro o acción correctos?

Scott B Scott B
16 ene 2011 01:04:48

Siendo esto WordPress, los hooks no necesariamente llevan el nombre de su función o clase/método relacionado, y tampoco necesariamente están en la prioridad 10 (por defecto)... Muchos lo están, pero muchos otros no.

Denis de Bernardy Denis de Bernardy
16 ene 2011 02:15:21
2

El filtro no intercepta la función, simplemente se conecta a un gancho existente. No hay ganchos para usar en ese método.

16 ene 2011 00:32:40
Comentarios

No estaba seguro, así que probé esa ruta primero. He colocado el código en la pregunta. Estoy intentando interceptar la rama is_404() para que el plugin no altere el título de mi página en ese caso.

Scott B Scott B
16 ene 2011 00:45:18

¿Entonces no hay manera de hacer esto?

Scott B Scott B
19 ene 2011 19:23:06