views:

272

answers:

2

The routine below inserts a page, sets permalinks to postname then attempts to update the permalinks settings.

However, when I view the newly created page, I still get a 404 error. The only way to make it go away is to hit the settings > permalinks manager (just merely accessing the page does the trick, i don't even have to save).

What's up with that?

//create and insert a post
$my_post['post_content'] = "This is the content";
$my_post['post_type'] = 'page';
$my_post['post_status'] = 'publish';
wp_insert_post($my_post);

// Including files responsible for .htaccess update require_once(ABSPATH . 'wp-admin/includes/misc.php'); require_once(ABSPATH . 'wp-admin/includes/file.php');

// Prepare WordPress Rewrite object in case it hasn't been initialized yet
if (empty($wp_rewrite) || !($wp_rewrite instanceof WP_Rewrite))
{
    $wp_rewrite = new WP_Rewrite();
}

// Update permalink structure
$permalink_structure = '/%postname%/';
$wp_rewrite->set_permalink_structure($permalink_structure);

// Recreate rewrite rules
$wp_rewrite->flush_rules();
A: 

Only a suggestion for you... Disable any WordPress cache functionality before debugging this problem. Often I have permalinks problems, when using any WP caching.

Dichev
Dichev, I'm using wp_cache_flush(); before executing my permalink code. That should do it no?
Scott B
A: 

Where is your code, in a plug-in or a theme?

The following plug-in works for me in the latest version of WP. It won't work if .htaccess is not writeable, but I don't think you can do anything about that.

function my_plugin_activate() {
    $test_post = array(
        'post_title' => 'Lorem ipsum',
        'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipisicing …',
        'post_status' => 'publish',
        'post_type' => 'post'
    );
    wp_insert_post($test_post);

    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%postname%/');
    $wp_rewrite->flush_rules();
}

register_activation_hook(__FILE__, 'my_plugin_activate');
Richard M
Code is in a plugin. I've got several posts that I'm creating via the script. should I call the rewrite code after each insert statement, or at the very end? Also, I'm using add_action( 'init','activate_plugin_init') but you are using register_activation_hook (what's the difference?)
Scott B
You should place the rewrite code after you've inserted all your posts, it should only need to run once. The activation hook function runs when a user clicks the activate link for the plug-in on the Wordpress plug-in options page. The `init` action you are hooking into runs on every request, this is it's description from the documentation: `runs after WordPress has finished loading but before any headers are sent. Useful for intercepting $_GET or $_POST triggers.`
Richard M
Richard - I'd like to use register_activation_hook, since my plugin only needs to run once and all inserts are done. No need to duplicate the inserts. So I want to use that rather than init, which sounds more expensive. HOWEVER, my routine only partially runs when I use that method...???
Scott B
CORRECTION > HOWEVER, I can't set variables outside the name function and reference them via $global inside the function (unless I'm missing something, which is entirely possible).
Scott B
With regards to variable scope in activation functions see my answer to this question: http://stackoverflow.com/questions/2397766/why-wont-my-global-variables-properly-resolve/2398106#2398106
Richard M
Richard, if you coded this as a theme, what would you have to do differently?
Scott B
I'm afraid I don't know. As far as I know there isn't anything like `register_activation_hook` for themes. I suppose you would have to go back to using a `init` action function, inside of which you would check/set an option to determine whether or not your theme had been activated.
Richard M
Have a look at this post for what might be a better solution: http://wpengineer.com/set-options-on-activation-themes/
Richard M