Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm adding custom pages in WordPress and trying to handle WordPress with my own CMS.

I'm using all WordPress' hooks to manage all these things. Now, I can make a custom post saying "a new album" but I don't know how to give a post name to it.

The database entry of post_name in the wp_posts table remains blank now as I don't know what to put there.

share|improve this question
1  
are you using wp_insert_post? –  birgire Feb 5 '13 at 10:26
    
@birgire: Thanks for reply. It works but there is one problem. It repeats post_name, but that should not be. –  Hiren Rathod Feb 5 '13 at 10:50

2 Answers 2

You can use sanitize_title() on the page title:

http://codex.wordpress.org/Function_Reference/sanitize_title

and then you might try something like this:

$my_page_title = "My page name";

$my_page_slug = sanitize_title($my_page_title); 
// or you can set the slug you want:
//$my_page_slug = "my-slug-for-this-page"; 

$my_page_content = "This is my page content";

// Create page object
$my_page = array(
  'post_title'    => $my_page_title,
  'post_name'    => $my_page_slug,
  'post_content'  => $my_page_content,
  'post_status'   => 'publish',
  'post_type'   => 'page',
  'post_author'   => 1,
);

// Insert the page into the database
wp_insert_post( $my_page );

When updating a page, assuming you have access to the page ID, use the following code to prevent duplicate entries:

$my_page = array(
  'ID'            => $my_page_id // current page ID
  'post_title'    => $my_page_title,
  'post_name'     => $my_page_slug,
  'post_content'  => $my_page_content,
  'post_status'   => 'publish',
  'post_type'     => 'page',
  'post_author'   => 1,
);

// Update page
wp_update_post( $my_page );

Note that you only need to pass changed values to wp_update_post (so if you've only changed the page content, you should pass $my_page_content only)

share|improve this answer
up vote 0 down vote accepted

Thanks for reply to both of you. I tried to use only sanitize_title function but it repeats when I want to add the same name post so I did something like below in my custom commands:

$post_name1 = sanitize_title($_POST['subcategory']); 
$post_name2 = wp_unique_post_slug($post_name1, $_POST['idofalbum'], 'publish', 'album', 0);

$post_name1 gives me title with dashes and $post_name2 gives me unique slug which I needed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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