• I want to know what script or code can be used to make a new page in word-press automatically, which can be binded to a theme back-end, I can take care of the back-end linking but i wanna know the code necessary to create a new page and also assign a template to it !

Viewing 3 replies - 1 through 3 (of 3 total)
  • First question is why not just supply an import file with the pages?

    The Harder Way
    This code has been altered as it was just for posts, so is not fully tested: so test on a local install!

    If you use it then please do update the pastebin for others to use ??

    https://digitalraindrops.pastebin.com/ayvGaLau

    What the functions should do is read two files from the themes directory, one with categories the other with posts or pages

    It will run once then set an option so it does not run every time.

    Categories
    $fileName = TEMPLATEPATH.'/demo-data/categories.txt';
    The function in the link above requires a TAB seperated file, you could create an array in code.

    Row 1 is the Headers Row two columns name and description

    Posts or pages
    $fileName = TEMPLATEPATH.'/demo-data/posts.txt';
    The function in the link above requires a TAB seperated file, you could create an array in code.

    Row 1 is the Headers Row Seven columns
    type, title, content ,excerpt, status, categories, tags

    type = post or page
    status = true (published) blank or false for draft
    categories = array comma seperated names
    tags = array comma seperated names

    I have not found where the template is set, it is likely in the post meta, someone else might add this??

    HTH

    David

    Thread Starter eurekastudioz

    (@eurekastudioz)

    What do you mean by import file ? Plus the code u gave me I don’t have any idea what to do with it.Can u please explain David. Plus if you wanna see this in action this sorta thing is done in DISPLAY theme on themeforest !

    Hi,
    The first example could be used to control and add several categories and posts to a site, that is why the content would be in files, and would be used for a specific template’s default data.

    I do not want to see this in action, but here is a cut down simple script to create a single page, please note this is UNTESTED CODE.

    Use a local WordPress environment and a child theme, add the code in the child theme’s functions.php, do not add any untested sample code to a live or online website without a local test, as you could get a fatal error.

    <?php
    
    /* First Admin Run Create the Default Page */
    if (is_admin() && !get_option('page-created')){
    	create_default_page();
    }
    
    function create_default_page(){
    	$tags=array('WordPress','Themes','Free Themes');
    
    	$args = array (
    		"post_type" => "page",
    		"post_title" => "About",
    		"post_content" => "This is my Content",
    		"post_excerpt" => "This is my Excerpt",
    		"post_status" => "published",
    		"post_author" => 1,
    	);
    
    	$postID = wp_insert_post($args);
    
    	if ($tags && $postID) wp_set_post_terms( $postID, $tags);
    
    	update_option('page-created',true);
    }
    
    ?>

    HTH

    David

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Make new page dynamically’ is closed to new replies.