0

When publishing a custom post type, I'm trying to override the custom permalink based on code rather than what the user types into the input box. I can confirm my action is firing correctly, but I'm suspecting it's being triggered before the update event, so the user's value then overrides mine.

add_action('publish_course-variation', 'update_course_variation_permalink', 10, 3);

function update_course_variation_permalink($course_variation_id){

  $cv_meta = get_post_meta($course_variation_id);
  $desired_permalink_value = "my/custom/permalink";

  if( isset($cv_meta['custom_permalink']) ){
      //HAS PERMALINK = CHECK AND UPDATE IF REQUIRED
      if( get_post_meta($course_variation_id, 'custom_permalink', true) != $desired_permalink_value ){
        update_post_meta($course_variation_id, 'custom_permalink', $desired_permalink_value); 
        $results = "UPDATED";
      }else{
        $results = "UNTOUCHED/ALREADY SET CORRECTLY";
      }
  }else{
    //DOESNT HAVE PERMALINK = ADD
    add_post_meta($course_variation_id, 'custom_permalink', $desired_permalink_value, true); 
    $results = "NEW CUSTOM PERMALINK CREATED";
  }



}//end function

How do I make my action happen after the post is updated? Or disable the permalink from being updated by hitting 'publish'?

Thanks

1

The publish_{post-type} action is triggered only when the post change from any post status (not published) to published; for example, if the post is already published and you edit it, the publish_{post-type} action is not triggered. I think you need to hook your function to save_post_{post_type} action, which is triggered every time a post is saved, not matter the status:

add_action('save_post_course-variation', 'update_course_variation_permalink', 10, 3);

function update_course_variation_permalink($course_variation_id){

  $cv_meta = get_post_meta($course_variation_id);
  $desired_permalink_value = "my/custom/permalink";

  if( isset($cv_meta['custom_permalink']) ){
      //HAS PERMALINK = CHECK AND UPDATE IF REQUIRED
      if( get_post_meta($course_variation_id, 'custom_permalink', true) != $desired_permalink_value ){
        update_post_meta($course_variation_id, 'custom_permalink', $desired_permalink_value); 
        $results = "UPDATED";
      }else{
        $results = "UNTOUCHED/ALREADY SET CORRECTLY";
      }
  }else{
    //DOESNT HAVE PERMALINK = ADD
    add_post_meta($course_variation_id, 'custom_permalink', $desired_permalink_value, true); 
    $results = "NEW CUSTOM PERMALINK CREATED";
  }

}//end function

Note: storing data in custom_permalink meta field doens't affect to the permalink. Not sure how you are handling the post permalink. I just answered your question about how to trigger the function when a post is updated. If you want to truly override permalink (not a meta field), you could use this (based on this answer):

add_filter('wp_insert_post_data', 'wpse_wp_insert_post_data', 10, 2);
function wpse_wp_insert_post_data($data, $post_attr) {

    $desired_permalink_value = "my/custom/permalink";

    if( ( isset( $data['post_name'] ) && $data['post_name'] != $desired_permalink_value ) || empty( $data['post_name'] ) ) {
        $data['post_name'] = $desired_permalink_value;
    }

    return $data;
}
  • Thanks but I'm still having the same problem. I'm able to trace that the hook is being triggered and the permalink meta data is being altered, however it gets overwritten by the value which is entered on the edit page. – Jiminy Cricket Apr 8 '15 at 23:56
  • btw, 'custom_permalink' is the the meta key which is used to override the permalink with a plugin called 'custom permalink'. – Jiminy Cricket Apr 8 '15 at 23:57
  • How could I know that you are using a plugin??? Anyway, how a plugin works or specific plugin issue is off-topic here. Your problem overriden the custom_permalink meta field was addressed and answered; how it affect to the plugin should be asked to the plugin developer. – cybmeta Apr 9 '15 at 6:39
  • Answer updated. – cybmeta Apr 9 '15 at 6:44
-3

You could just do a display:none on the css selector for the slug so they never see it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.