$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>taxonomy - I want to change the slugs of my terms dynamically|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

taxonomy - I want to change the slugs of my terms dynamically

matteradmin9PV0评论

I'm looking to change my slugs dynamically so that they are displayed with the names of my terms, for that I created an option in the administrator panel that should allow me to start the process, but I do not really know where to start.

For starters I think I'll have to recover the taxonomies in which I find all the terms that I will have to dynamically change.

Would someone have a sample code that I could exploit for this function that i'm trying to create?

here are the taxonomies in which I will have to go to get my terms to modify these dynamically::

Taxonomies: recipe_category, recipe_type, recipe_event, product_category

here's the code that will allow me to start the process:

    <?php
add_action( 'admin_menu', 'slug_migrate_menu' );
function slug_migrate_menu() {
    add_submenu_page('edit.php?post_type=recipe', 'Migration des slugs', 'Migration des slugs', 'manage_options', 'slug-migrate', 'slug_migrate_page');
}
function slug_migrate_page() {
?>
<h1>Migration des slugs</h1>
<button id="migrateSlugBtn">Go !</button>
<div id="migrateSlugResult"></div>
<script>
jQuery('#migrateSlugBtn').click(function(e) {
    jQuery.post(
        '<?php echo admin_url( 'admin-ajax.php' ); ?>',
        {
            'action': 'slug_migrate',
        },
        function(response){
            jQuery('#migrateSlugResult').append(response);
        }
    );
});
</script>
<?php
}
add_action( 'wp_ajax_slug_migrate', 'slug_migrate' );
add_action( 'wp_ajax_nopriv_slug_migrate', 'slug_migrate' );

function slug_migrate( $taxonomy ) {
    // Taxonomies : recipe_category, recipe_type, recipe_event, product_category
    // Pour chaque term des taxonomies : Effectuer un sanitize_title() du name du term et l'afficher


              // begin of code





              // end of code


        add_filter( 'editable_slug', 'sanitize_slug' );


    die();
}

PS: Here is what I have done for now, I do not know if it's a good reasoning, and any help would be welcome:

$terms = get_terms( $post->ID, $catslug);?>

          <?php foreach($terms as $term) : ?>

                <?php if($term->slug !== $term->name){

                    $find = array(
                        $term->slug
                    );

                    $replace = array(
                        $term->name
                    );

                    $replace = array_map( 'sanitize_title', $replace );

                    $permalink = str_replace( $find, $replace, $permalink );


                    echo $permalink;

                }?>

          <?php
            endforeach;
          ?>

Hi @chrisbergr

thank you for your help, I made a montage (image) of what I am trying to do (in fact, I would like to change the names of all of my terms dynamically, instead of having to do them manually):

I'm looking to change my slugs dynamically so that they are displayed with the names of my terms, for that I created an option in the administrator panel that should allow me to start the process, but I do not really know where to start.

For starters I think I'll have to recover the taxonomies in which I find all the terms that I will have to dynamically change.

Would someone have a sample code that I could exploit for this function that i'm trying to create?

here are the taxonomies in which I will have to go to get my terms to modify these dynamically::

Taxonomies: recipe_category, recipe_type, recipe_event, product_category

here's the code that will allow me to start the process:

    <?php
add_action( 'admin_menu', 'slug_migrate_menu' );
function slug_migrate_menu() {
    add_submenu_page('edit.php?post_type=recipe', 'Migration des slugs', 'Migration des slugs', 'manage_options', 'slug-migrate', 'slug_migrate_page');
}
function slug_migrate_page() {
?>
<h1>Migration des slugs</h1>
<button id="migrateSlugBtn">Go !</button>
<div id="migrateSlugResult"></div>
<script>
jQuery('#migrateSlugBtn').click(function(e) {
    jQuery.post(
        '<?php echo admin_url( 'admin-ajax.php' ); ?>',
        {
            'action': 'slug_migrate',
        },
        function(response){
            jQuery('#migrateSlugResult').append(response);
        }
    );
});
</script>
<?php
}
add_action( 'wp_ajax_slug_migrate', 'slug_migrate' );
add_action( 'wp_ajax_nopriv_slug_migrate', 'slug_migrate' );

function slug_migrate( $taxonomy ) {
    // Taxonomies : recipe_category, recipe_type, recipe_event, product_category
    // Pour chaque term des taxonomies : Effectuer un sanitize_title() du name du term et l'afficher


              // begin of code





              // end of code


        add_filter( 'editable_slug', 'sanitize_slug' );


    die();
}

PS: Here is what I have done for now, I do not know if it's a good reasoning, and any help would be welcome:

$terms = get_terms( $post->ID, $catslug);?>

          <?php foreach($terms as $term) : ?>

                <?php if($term->slug !== $term->name){

                    $find = array(
                        $term->slug
                    );

                    $replace = array(
                        $term->name
                    );

                    $replace = array_map( 'sanitize_title', $replace );

                    $permalink = str_replace( $find, $replace, $permalink );


                    echo $permalink;

                }?>

          <?php
            endforeach;
          ?>

Hi @chrisbergr

thank you for your help, I made a montage (image) of what I am trying to do (in fact, I would like to change the names of all of my terms dynamically, instead of having to do them manually):

Share Improve this question edited Feb 25, 2019 at 13:16 Frenchy_WP asked Feb 25, 2019 at 9:07 Frenchy_WPFrenchy_WP 191 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

As far as I got you right, the following code is all you need to solve this.

function my_function( $post_id ){
if ( ! wp_is_post_revision( $post_id ) ){
    $post = get_post( $post_id );
    $my_args = array(
        'ID'        => $post_id,
        'post_name' => ''
    );
    // unhook this function so it doesn't loop infinitely
    remove_action( 'save_post', 'my_function' );
    // update the post, which calls save_post again
    wp_update_post( $my_args );
    // re-hook this function
    add_action( 'save_post', 'my_function' );
}
}
add_action( 'save_post', 'my_function' );

See wp_update_post in the codex.

I just tested the code in WordPress 5.1 with the block editor.

Post a comment

comment list (0)

  1. No comments so far