Actualiza el SEO desde una tabla usando este shorcode para Yoast SEO y Rank Math

Tabla de contenidos

Este shorcode te muestra una parte muy importante del SEO de tus CPT, pudiendo editar fácilmente el Título, URL, Keyword, Title, Description, así como asignarle un ALT a tu imagen destacada.

Puedes usar varios CPT añadiendo en el shorcode el tipo de cpt.

Uso del shortcode para YOAST:

info seo yoast

Uso del shortcode para Rank Math:

info seo rank math

 

Captura de pantalla 2023 07 21 a las 15.06.42

Shorcode con plugin Yoast SEO

function infoseo_implementar_yoast_shortcode($atts) {
    $cpt = isset($atts['cpt']) ? $atts['cpt'] : '';
    $current_page = isset($_GET['infoseo_page']) ? $_GET['infoseo_page'] : 1;
    $current_page = ($current_page) ? $current_page : 1; // Asegurarse de que la página 1 esté seleccionada por defecto
    $posts_per_page = 50;
    $offset = ($current_page - 1) * $posts_per_page;

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        foreach ($_POST as $key => $value) {
            if (strpos($key, 'update_') !== false) {
                $post_id = str_replace('update_', '', $key);
                $thumbnail_id = get_post_thumbnail_id($post_id);

                wp_update_post(array(
                    'ID' => $post_id,
                    'post_title' => $_POST['title_'.$post_id],
                    'post_name' => str_replace(' ', '/', $_POST['slug_'.$post_id]) // Se reemplazan los espacios con '/'
                ));
                

                update_post_meta($post_id, '_yoast_wpseo_focuskw', $_POST['keyword_'.$post_id]);
                update_post_meta($post_id, '_yoast_wpseo_title', $_POST['seo_title_'.$post_id]);
                update_post_meta($post_id, '_yoast_wpseo_metadesc', $_POST['meta_description_'.$post_id]);

                if ($thumbnail_id) {
                    update_post_meta($thumbnail_id, '_wp_attachment_image_alt', $_POST['alt_image_'.$post_id]);
                }
            }
        }
    }

    $args = array(
        'post_type' => $cpt,
        'posts_per_page' => $posts_per_page,
        'offset' => $offset,
        'post__not_in' => array(get_queried_object_id()),
    );

    $posts = get_posts($args);

    $total_posts = wp_count_posts($cpt)->publish;
    $total_pages = ceil($total_posts / $posts_per_page);

    $output = '';

    $output .= '<style>';
    $output .= 'table.informe-yoast-edit input, table.informe-yoast-edit textarea {font-size: 12px;padding: 4px;display: block;}';
    $output .= '.missing-data { background-color: #f2c9c9; }';
    $output .= '.data-invalid { background-color: #ffe1e1; }';
    $output .= 'table {width: 100%; border-collapse: collapse;}';
    $output .= 'th {background-color: #4CAF50; color: white;}';
    $output .= 'td, th {border: 1px solid #ddd; padding: 15px;}';
    $output .= 'tr:nth-child(even) {background-color: #f2f2f2;}';
    $output .= 'span.cantidad {position: absolute;margin-top: -17px;font-size: 10px;margin-left: -1px;}';
    $output .= 'span.ver a {background: #000;color: #fff;font-size: 10px;padding: 2px;text-decoration: none;left: 0;}';
    $output .= 'span.ver.editar a {background: #7c7c7c;margin-left: 2px;}';
    $output .= '.current {background-color: #4CAF50!important;color: white;}';
    $output .= '.paginationinfo {text-align: center;}';
    $output .= '.paginationinfo a {padding: 4px;background: #efefef;margin: 1px;text-decoration: none;font-size: 14px;}';
    $output .= '.paginationinfo a:hover, .paginationinfo a.current {background: #4caf4f;color: #fff;}';
    $output .= '</style>';

    $output .= '<table class="informe-yoast-edit" style="font-size: 12px;">';
    $output .= '<tr>';
    $output .= '<th>Título</th>';
    $output .= '<th>Slug</th>';
    $output .= '<th>Keyword</th>';
    $output .= '<th>Título SEO <br><small>(30 - 60)</small></th>';
    $output .= '<th>Meta Description <br><small>(120 - 160)</small></th>';
    $output .= '<th>Imagen</th>';
    $output .= '<th>Slug de imagen</th>';
    $output .= '<th>Alt de imagen</th>';
    $output .= '<th>Actualizar</th>';
    $output .= '</tr>';

foreach ($posts as $post) {
    $title = get_the_title($post->ID);
    //$slug = str_replace(home_url(), '', get_permalink($post->ID));
    
    $slug = str_replace('/', ' ', str_replace(home_url(), '', get_permalink($post->ID)));

    $keyword = get_post_meta($post->ID, '_yoast_wpseo_focuskw', true);
    $seo_title = get_post_meta($post->ID, '_yoast_wpseo_title', true);
    $meta_description = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
    $thumbnail_id = get_post_thumbnail_id($post->ID);
    $thumbnail = $thumbnail_id ? get_the_post_thumbnail_url($post->ID, 'thumbnail') : '';
    $slug_image = $thumbnail ? basename($thumbnail) : '';
    $alt_image = $thumbnail ? get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true) : '';

    $isInvalidRow = false;
    if(empty($title) || empty($slug) || empty($keyword) || empty($seo_title) || empty($meta_description) || empty($thumbnail) || empty($slug_image) || empty($alt_image)) {
        $isInvalidRow = true;
    }

    $seoTitleLength = strlen($seo_title);
    $isInvalidSeoTitle = $seoTitleLength < 30 || $seoTitleLength > 60;

    $metaDescriptionLength = strlen($meta_description);
    $isInvalidMetaDescription = $metaDescriptionLength < 120 || $metaDescriptionLength > 160;

    $output .= '<form method="post"><tr' . ($isInvalidRow ? ' class="data-invalid"' : '') . '>';
    $output .= '<td><input type="text" name="title_'.$post->ID.'" value="'.$title.'"'.(empty($title) ? ' class="data-invalid"' : '').'><span class="ver"><a href="'. get_permalink($post->ID) .'" target="_blank">Ver Post</a></span><span class="ver editar"><a href="'. get_edit_post_link($post->ID) .'" target="_blank">Editar Post</a></span></td>';
    $output .= '<td><input type="text" name="slug_'.$post->ID.'" value="'.$slug.'"'.(empty($slug) ? ' class="data-invalid"' : '').'></td>';
    $output .= '<td><input type="text" name="keyword_'.$post->ID.'" value="'.$keyword.'"'.(empty($keyword) ? ' class="data-invalid"' : '').'></td>';
    $output .= '<td><span class="cantidad">'. strlen($seo_title) . '</span><textarea name="seo_title_'.$post->ID.'"'.($isInvalidSeoTitle ? ' class="data-invalid"' : '').'>'.$seo_title.'</textarea></td>';
    $output .= '<td><span class="cantidad">'. strlen($meta_description) . '</span><textarea name="meta_description_'.$post->ID.'"'.($isInvalidMetaDescription ? ' class="data-invalid"' : '').'>'.$meta_description.'</textarea></td>';

    $output .= $thumbnail ? '<td><img src="' . $thumbnail . '" style="width:50px;height:auto;" /></td>' : '<td class="data-invalid"></td>';
    $output .= '<td' . (empty($slug_image) ? ' class="data-invalid"' : '') . '>' . $slug_image . '</td>';
    
    $output .= $thumbnail ? '<td><input type="text" name="alt_image_'.$post->ID.'" value="'.$alt_image.'"'.(empty($alt_image) ? ' class="data-invalid"' : '').'></td>' : '<td class="data-invalid"></td>';
    

    $output .= '<td><button type="submit" name="update_'.$post->ID.'">Actualizar</button></td>';
    $output .= '</tr></form>';
}

$output .= '</table>';

    // Implementación de paginación
    $output .= '<div class="paginationinfo">';
    for ($i = 1; $i <= $total_pages; $i++) {
        $current = ($i == $current_page) ? ' class="current"' : '';
        $output .= '<a href="?infoseo_page=' . $i . '"' . $current . '>' . $i . '</a>';
    }
    $output .= '</div>';

    return $output;
}
add_shortcode('infoseo_editable_yoast', 'infoseo_implementar_yoast_shortcode');

 

Sortcode con plugin Rank Math

function infoseo_implementar_rankmath_shortcode($atts) {
    $cpt = isset($atts['cpt']) ? $atts['cpt'] : '';
    $current_page = isset($_GET['infoseo_page']) ? $_GET['infoseo_page'] : 1;
    $current_page = ($current_page) ? $current_page : 1; 
    $posts_per_page = 50;
    $offset = ($current_page - 1) * $posts_per_page;

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        foreach ($_POST as $key => $value) {
            if (strpos($key, 'update_') !== false) {
                $post_id = str_replace('update_', '', $key);
                $thumbnail_id = get_post_thumbnail_id($post_id);

                wp_update_post(array(
                    'ID' => $post_id,
                    'post_title' => $_POST['title_'.$post_id],
                    'post_name' => str_replace(' ', '/', $_POST['slug_'.$post_id])
                ));

                update_post_meta($post_id, 'rank_math_focus_keyword', $_POST['keyword_'.$post_id]);
                update_post_meta($post_id, 'rank_math_title', $_POST['seo_title_'.$post_id]);
                update_post_meta($post_id, 'rank_math_description', $_POST['meta_description_'.$post_id]);

                if ($thumbnail_id) {
                    update_post_meta($thumbnail_id, '_wp_attachment_image_alt', $_POST['alt_image_'.$post_id]);
                }
            }
        }
    }

    $args = array(
        'post_type' => $cpt,
        'posts_per_page' => $posts_per_page,
        'offset' => $offset,
        'post__not_in' => array(get_queried_object_id()),
    );

    $posts = get_posts($args);

    $total_posts = wp_count_posts($cpt)->publish;
    $total_pages = ceil($total_posts / $posts_per_page);

    $output = '';

    $output .= '<style>';
    $output .= 'table.informe-rankmath-edit input, table.informe-rankmath-edit textarea {font-size: 12px;padding: 4px;display: block;}';
    $output .= '.missing-data { background-color: #f2c9c9; }';
    $output .= '.data-invalid { background-color: #ffe1e1; }';
    $output .= 'table {width: 100%; border-collapse: collapse;}';
    $output .= 'th {background-color: #4CAF50; color: white;}';
    $output .= 'table.informe-rankmath-edit tr th{background-color: #4CAF50!important}';
    $output .= 'td, th {border: 1px solid #ddd; padding: 15px;}';
    $output .= 'tr:nth-child(even) {background-color: #f2f2f2;}';
    $output .= 'span.cantidad {position: absolute;margin-top: -17px;font-size: 10px;margin-left: -1px;}';
    $output .= 'span.ver a {background: #000;color: #fff;font-size: 10px;padding: 2px;text-decoration: none;left: 0;}';
    $output .= 'span.ver.editar a {background: #7c7c7c;margin-left: 2px;}';
    $output .= '.current {background-color: #4CAF50!important;color: white;}';
    $output .= '.paginationinfo {text-align: center;}';
    $output .= '.paginationinfo a {padding: 4px;background: #efefef;margin: 1px;text-decoration: none;font-size: 14px;}';
    $output .= '.paginationinfo a:hover, .paginationinfo a.current {background: #4caf4f;color: #fff;}';
    $output .= '</style>';

    $output .= '<table class="informe-rankmath-edit" style="font-size: 12px;">';
    $output .= '<tr>';
    $output .= '<th>Título</th>';
    $output .= '<th>Slug</th>';
    $output .= '<th>Keyword</th>';
    $output .= '<th>Título SEO <br><small>(30 - 60)</small></th>';
    $output .= '<th>Meta Description <br><small>(120 - 160)</small></th>';
    $output .= '<th>Imagen</th>';
    $output .= '<th>Slug de imagen</th>';
    $output .= '<th>Alt de imagen</th>';
    $output .= '<th>Actualizar</th>';
    $output .= '</tr>';

    foreach ($posts as $post) {
        $title = get_the_title($post->ID);
        $slug = str_replace('/', ' ', str_replace(home_url(), '', get_permalink($post->ID)));
        $keyword = get_post_meta($post->ID, 'rank_math_focus_keyword', true);
        $seo_title = get_post_meta($post->ID, 'rank_math_title', true);
        $meta_description = get_post_meta($post->ID, 'rank_math_description', true);
        $thumbnail_id = get_post_thumbnail_id($post->ID);
        $thumbnail = $thumbnail_id ? get_the_post_thumbnail_url($post->ID) : '';
        $thumbnailmini = $thumbnail_id ? get_the_post_thumbnail_url($post->ID, 'thumbnail') : '';
        $slug_image = $thumbnail ? basename($thumbnail) : '';
        $alt_image = $thumbnail ? get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true) : '';

        $isInvalidRow = false;
        if(empty($title) || empty($slug) || empty($keyword) || empty($seo_title) || empty($meta_description) || empty($thumbnail) || empty($slug_image) || empty($alt_image)) {
            $isInvalidRow = true;
        }

        $seoTitleLength = strlen($seo_title);
        $isInvalidSeoTitle = $seoTitleLength < 30 || $seoTitleLength > 60;

        $metaDescriptionLength = strlen($meta_description);
        $isInvalidMetaDescription = $metaDescriptionLength < 120 || $metaDescriptionLength > 160;

        $output .= '<form method="post"><tr' . ($isInvalidRow ? ' class="data-invalid"' : '') . '>';
        $output .= '<td><input type="text" name="title_'.$post->ID.'" value="'.$title.'"'.(empty($title) ? ' class="missing-data"' : '').'><span class="ver"><a href="'. get_permalink($post->ID) .'" target="_blank">Ver Post</a></span><span class="ver editar"><a href="'. get_edit_post_link($post->ID) .'" target="_blank">Editar Post</a></span></td>';
        $output .= '<td><input type="text" name="slug_'.$post->ID.'" value="'.$slug.'"'.(empty($slug) ? ' class="missing-data"' : '').'></td>';
        $output .= '<td><input type="text" name="keyword_'.$post->ID.'" value="'.$keyword.'"'.(empty($keyword) ? ' class="missing-data"' : '').'></td>';
        $output .= '<td><span class="cantidad">'.$seoTitleLength.'</span><textarea name="seo_title_'.$post->ID.'"'.($isInvalidSeoTitle ? ' class="missing-data"' : '').'>'.$seo_title.'</textarea></td>';
        $output .= '<td><span class="cantidad">'.$metaDescriptionLength.'</span><textarea name="meta_description_'.$post->ID.'"'.($isInvalidMetaDescription ? ' class="missing-data"' : '').'>'.$meta_description.'</textarea></td>';
        $output .= '<td>'.($thumbnailmini ? '<img src="'.$thumbnailmini.'">' : '').'</td>';
        $output .= '<td>'.($thumbnail ? $slug_image : '').'</td>';
        $output .= '<td><input type="text" name="alt_image_'.$post->ID.'" value="'.$alt_image.'"'.(empty($alt_image) ? ' class="missing-data"' : '').'></td>';
        $output .= '<td><input style="width:100%" type="submit" name="update_'.$post->ID.'" value="Actualizar"></td>';
        $output .= '</tr></form>';
    }

    $output .= '</table>';

    $output .= '<div class="paginationinfo">';
    for ($i = 1; $i <= $total_pages; $i++) {
        $output .= '<a href="?infoseo_page='.$i.'"'.($i == $current_page ? ' class="current"' : '').'>'.$i.'</a>';
    }
    $output .= '</div>';

    return $output;
}
add_shortcode('infoseo_editable_rankmath', 'infoseo_implementar_rankmath_shortcode');

 

 

Picture of Jose Hilario
Jose Hilario
¡Apasionado del diseño web! Especializado en WordPress, WooCommerce y Elementor PRO. Me encanta diseñar páginas web atractivas y optimizadas para SEO. Mi objetivo: impulsar a las empresas con un diseño web atractivo y funcional.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Relacionados: