• hey, I’m trying to add some filters to the post slug (aka post name) but the ‘name_save_pre’ filter isn’t being called when I save a post. Am I missing something?

    function mhp_change_name($p){
    
    	//printing parameter so I can see it's being called
    	print_r($p);
    
    	return $p;
    }
    
    add_filter('name_save_pre', 'mhp_change_name', 10, 1);
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter sevennine

    (@sevennine)

    ok, after some brute force and ignorance I got something working. My only question is why doesn’t name_save_pre get called the first time I save a post or when I change its category. I have to click save twice to get it to work.

    For reference, here’s the skeleton of my working code. It’s interesting because post savings are actually redirected to the post.php file but with no attributes. Once it’s saved, you’re redirected to the editing screen. So there’s a secret page that saves the post. It makes debugging a little harder.

    function mhp_change_slug($slug) {
    	$new_slug = $slug;
    
    	//get post since it's not global
    	$post = get_post($_POST['post_ID']);
    
    	//filtering magic goes here
    
    	//return new slug
    	return $new_slug;
    }
    
    add_filter('name_save_pre', 'mhp_change_slug', 1, 1);

    so does anyone know why ‘name_save_pre’ doesn’t get called on the initial save?

    I just tackled this problem as well, so I’ll post this for anyone wondering the same thing.

    I’m not sure about it not being called at all (not activating print_r() — this worked for me). However, at first I thought it wasn’t being called as well. The real problem was that I was trying to use get_post_meta() to pull terms from custom fields, but the post hadn’t been saved in the database yet. I got around this by using the $_POST array to collect the same information. Here’s the code I ended up using:

    add_filter('name_save_pre', 'save_name');
    function save_name($my_post_name) {
            if ($_POST['post_type'] == 'parties') :
              $party_date = $_POST['date'];
              $party_name = $_POST['post_title'];
              $my_post_name = $party_name . "-" . $party_date;
            endif;
            return $my_post_name;
    }

    This checks to see if the post type is ‘parties’, and if so, it modifies the post slug (URL) to add the date to it. It seems to be working without error so far.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘name_save_pre not being called’ is closed to new replies.