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

customization - Adding a custom field to a slug

programmeradmin4浏览0评论

I'm using the WP Job Manager plugin, and want to add the company name field to the slug automatically when I save a post.

I've got it working using the code below, but once I save and then edit the post again, it will re-add the company name, so job-title-company-name becomes job-title-company-name-company-name

Does anyone know what I'm doing wrong? I think there might be an issue with my IF statement which should check if the companyname string already exists in the slug, and if so, unhook the function.

function custom_job_post_type_link( $post_id, $post ) {

    $permalink = $post->post_name;  
    $companyname = $post->_company_name;

    if ( strpos( $permalink, strval($companyname) ) ) {
        return;
    }
    
    // unhook this function to prevent infinite looping
    remove_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );

    // add the id to the slug
    $permalink .= '-' . $companyname;

    // update the post slug
    wp_update_post( array(
        'ID' => $post_id,
        'post_name' => $permalink
    ));

    // re-hook this function
    add_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );
}

add_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );

I'm using the WP Job Manager plugin, and want to add the company name field to the slug automatically when I save a post.

I've got it working using the code below, but once I save and then edit the post again, it will re-add the company name, so job-title-company-name becomes job-title-company-name-company-name

Does anyone know what I'm doing wrong? I think there might be an issue with my IF statement which should check if the companyname string already exists in the slug, and if so, unhook the function.

function custom_job_post_type_link( $post_id, $post ) {

    $permalink = $post->post_name;  
    $companyname = $post->_company_name;

    if ( strpos( $permalink, strval($companyname) ) ) {
        return;
    }
    
    // unhook this function to prevent infinite looping
    remove_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );

    // add the id to the slug
    $permalink .= '-' . $companyname;

    // update the post slug
    wp_update_post( array(
        'ID' => $post_id,
        'post_name' => $permalink
    ));

    // re-hook this function
    add_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );
}

add_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );
Share Improve this question edited Oct 3, 2020 at 16:09 geouser 3471 silver badge7 bronze badges asked Oct 3, 2020 at 14:56 michael74michael74 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try to use function sanitize_title() - documentation, which converts a string into a slug when you check if it is already in the permalink, and when appending it to permalink:

function custom_job_post_type_link( $post_id, $post ) {

    $permalink = $post->post_name;  
    $companyname = $post->_company_name;

    if ( strpos( $permalink, sanitize_title($companyname) ) ) { // <-- Here
        return;
    }
    
    // unhook this function to prevent infinite looping
    remove_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );

    // add the id to the slug
    $permalink .= '-' . sanitize_title($companyname); // <-- And here

    // update the post slug
    wp_update_post( array(
        'ID' => $post_id,
        'post_name' => $permalink
    ));

    // re-hook this function
    add_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );
}

add_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );
发布评论

评论列表(0)

  1. 暂无评论