0

NEW EDIT I solved it, as written in answer, the problem where that variables are 'forgotten' (not so techy I know, sorry for this) when function starts.

EDIT: I found the problem is this: 'name' => _x( $plurcpost, $singcpost ), if I use text instead of a variable everything works properly.... So, How can I use a variable here?

I am working on a new WordPress template using wptheming Option Framework. User can choose how many custom posts he want (between 1 to 5) using a radio select. The value of the radio is stored in DB and then, inside functions.php there is a require that 'includes' the proper PHP file. The 'include' works as expected ( I tryied to delete this: <?php at the beginning of each file and I can see the included files on top of my admin screen). I thought that the problem could be the option not saved but, I tryied to echo each of them and I had no problem. So I think that I am doing something wrong in the file that generates the cPosts.

This is what I have in functions.php:

/* Include Custom-posts settings */
if (of_get_option('custom_custom_post_creator')){//this is working
    $quanticpost = of_get_option('how_many_custom_posts');//this is working too
require_once MDCP_DIR . 'cpost/cpost_if_'.$quanticpost.'.php';//this is working too
};

This is cpost_if_5.php :

<?php
    /* IF 1 Start */
    $singcpost = of_get_option('custom_posts_name_s_n1');
    $plurcpost = of_get_option('custom_posts_name_p_n1');
    $desccpost = of_get_option('custom_posts_name_d_n1');
    $imgcpost  = of_get_option('custom_posts_name_i_n1');
add_action( 'init', 'register_cpt_cpost_mdframework_1' );

function register_cpt_cpost_mdframework_1() {

    $labels = array( 
        'name' => _x( $plurcpost, $singcpost ),
        'singular_name' => _x( $singcpost, $singcpost ),
        'add_new' => _x( 'Add New', $singcpost ),
        'add_new_item' => _x( 'Add New '.$singcpost.'', $singcpost ),
        'edit_item' => _x( 'Edit '.$singcpost.'', $singcpost ),
        'new_item' => _x( 'New '.$singcpost.'', $singcpost ),
        'view_item' => _x( 'View '.$singcpost.'', $singcpost ),
        'search_items' => _x( 'Search '.$plurcpost.'', $singcpost ),
        'not_found' => _x( 'No '.$plurcpost.' found', $singcpost ),
        'not_found_in_trash' => _x( 'No '.$plurcpost.' found in Trash', $singcpost ),
        'parent_item_colon' => _x( 'Parent '.$singcpost.':', $singcpost ),
        'menu_name' => _x( $plurcpost, $singcpost ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => $desccpost,
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category', 'post_tag', 'page-category', 'customtax1', 'customtax2', 'customtax3' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => $imgcpost,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( $singcpost, $args );
}
/* IF 1 END */
    /* IF 2 Start */
    $singcpost = of_get_option('custom_posts_name_s_n2');
    $plurcpost = of_get_option('custom_posts_name_p_n2');
    $desccpost = of_get_option('custom_posts_name_d_n2');
    $imgcpost  = of_get_option('custom_posts_name_i_n2');
add_action( 'init', 'register_cpt_cpost_mdframework_2' );

function register_cpt_cpost_mdframework_2() {

    $labels = array( 
        'name' => _x( $plurcpost, $singcpost ),
        'singular_name' => _x( $singcpost, $singcpost ),
        'add_new' => _x( 'Add New', $singcpost ),
        'add_new_item' => _x( 'Add New '.$singcpost.'', $singcpost ),
        'edit_item' => _x( 'Edit '.$singcpost.'', $singcpost ),
        'new_item' => _x( 'New '.$singcpost.'', $singcpost ),
        'view_item' => _x( 'View '.$singcpost.'', $singcpost ),
        'search_items' => _x( 'Search '.$plurcpost.'', $singcpost ),
        'not_found' => _x( 'No '.$plurcpost.' found', $singcpost ),
        'not_found_in_trash' => _x( 'No '.$plurcpost.' found in Trash', $singcpost ),
        'parent_item_colon' => _x( 'Parent '.$singcpost.':', $singcpost ),
        'menu_name' => _x( $plurcpost, $singcpost ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => $desccpost,
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category', 'post_tag', 'page-category', 'customtax1', 'customtax2', 'customtax3' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => $imgcpost,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( $singcpost, $args );
}
/* IF 2 END */
    /* IF 3 Start */
    $singcpost = of_get_option('custom_posts_name_s_n3');
    $plurcpost = of_get_option('custom_posts_name_p_n3');
    $desccpost = of_get_option('custom_posts_name_d_n3');
    $imgcpost  = of_get_option('custom_posts_name_i_n3');
add_action( 'init', 'register_cpt_cpost_mdframework_3' );

function register_cpt_cpost_mdframework_3() {

    $labels = array( 
        'name' => _x( $plurcpost, $singcpost ),
        'singular_name' => _x( $singcpost, $singcpost ),
        'add_new' => _x( 'Add New', $singcpost ),
        'add_new_item' => _x( 'Add New '.$singcpost.'', $singcpost ),
        'edit_item' => _x( 'Edit '.$singcpost.'', $singcpost ),
        'new_item' => _x( 'New '.$singcpost.'', $singcpost ),
        'view_item' => _x( 'View '.$singcpost.'', $singcpost ),
        'search_items' => _x( 'Search '.$plurcpost.'', $singcpost ),
        'not_found' => _x( 'No '.$plurcpost.' found', $singcpost ),
        'not_found_in_trash' => _x( 'No '.$plurcpost.' found in Trash', $singcpost ),
        'parent_item_colon' => _x( 'Parent '.$singcpost.':', $singcpost ),
        'menu_name' => _x( $plurcpost, $singcpost ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => $desccpost,
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category', 'post_tag', 'page-category', 'customtax1', 'customtax2', 'customtax3' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => $imgcpost,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( $singcpost, $args );
}
/* IF 3 END */
    /* IF 4 Start */
    $singcpost = of_get_option('custom_posts_name_s_n4');
    $plurcpost = of_get_option('custom_posts_name_p_n4');
    $desccpost = of_get_option('custom_posts_name_d_n4');
    $imgcpost  = of_get_option('custom_posts_name_i_n4');
add_action( 'init', 'register_cpt_cpost_mdframework_4' );

function register_cpt_cpost_mdframework_4() {

    $labels = array( 
        'name' => _x( $plurcpost, $singcpost ),
        'singular_name' => _x( $singcpost, $singcpost ),
        'add_new' => _x( 'Add New', $singcpost ),
        'add_new_item' => _x( 'Add New '.$singcpost.'', $singcpost ),
        'edit_item' => _x( 'Edit '.$singcpost.'', $singcpost ),
        'new_item' => _x( 'New '.$singcpost.'', $singcpost ),
        'view_item' => _x( 'View '.$singcpost.'', $singcpost ),
        'search_items' => _x( 'Search '.$plurcpost.'', $singcpost ),
        'not_found' => _x( 'No '.$plurcpost.' found', $singcpost ),
        'not_found_in_trash' => _x( 'No '.$plurcpost.' found in Trash', $singcpost ),
        'parent_item_colon' => _x( 'Parent '.$singcpost.':', $singcpost ),
        'menu_name' => _x( $plurcpost, $singcpost ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => $desccpost,
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category', 'post_tag', 'page-category', 'customtax1', 'customtax2', 'customtax3' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => $imgcpost,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( $singcpost, $args );
}
/* IF 4 END */
    /* IF 5 Start */
    $singcpost = of_get_option('custom_posts_name_s_n5');
    $plurcpost = of_get_option('custom_posts_name_p_n5');
    $desccpost = of_get_option('custom_posts_name_d_n5');
    $imgcpost  = of_get_option('custom_posts_name_i_n5');
add_action( 'init', 'register_cpt_cpost_mdframework_5' );

function register_cpt_cpost_mdframework_5() {

    $labels = array( 
        'name' => _x( $plurcpost, $singcpost ),
        'singular_name' => _x( $singcpost, $singcpost ),
        'add_new' => _x( 'Add New', $singcpost ),
        'add_new_item' => _x( 'Add New '.$singcpost.'', $singcpost ),
        'edit_item' => _x( 'Edit '.$singcpost.'', $singcpost ),
        'new_item' => _x( 'New '.$singcpost.'', $singcpost ),
        'view_item' => _x( 'View '.$singcpost.'', $singcpost ),
        'search_items' => _x( 'Search '.$plurcpost.'', $singcpost ),
        'not_found' => _x( 'No '.$plurcpost.' found', $singcpost ),
        'not_found_in_trash' => _x( 'No '.$plurcpost.' found in Trash', $singcpost ),
        'parent_item_colon' => _x( 'Parent '.$singcpost.':', $singcpost ),
        'menu_name' => _x( $plurcpost, $singcpost ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => $desccpost,
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category', 'post_tag', 'page-category', 'customtax1', 'customtax2', 'customtax3' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => $imgcpost,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( $singcpost, $args );
}
/* IF 5 END */
    ;?>

This is what I see in Back-end even if, in theory, 5 custom posts are active. This is what I can see in the backend

This is the url if I try to add a custom post:

wp-admin/post-new.php?post_type=

I think that the problem is related with my custom post code but I can't find what I did wrong. Maybe variables aren't read as I thought by custom post code?

2

Ok, I solved it, maybe instead of asking I should drink 1 more coffe or sleep something more, anyway the solution is to move this part:

$singcpost = of_get_option('custom_posts_name_s_n1');
$plurcpost = of_get_option('custom_posts_name_p_n1');
$desccpost = of_get_option('custom_posts_name_d_n1');
$imgcpost  = of_get_option('custom_posts_name_i_n1');

After the opening function...

Here is the corrected code:

<?php
    /* IF 1 Start */
add_action( 'init', 'register_cpt_cpost_mdframework_1' );

function register_cpt_cpost_mdframework_1() {
    $singcpost = of_get_option('custom_posts_name_s_n1');
    $plurcpost = of_get_option('custom_posts_name_p_n1');
    $desccpost = of_get_option('custom_posts_name_d_n1');
    $imgcpost  = of_get_option('custom_posts_name_i_n1');
    $labels = array( 
        'name' => _x( $plurcpost, $singcpost ),
        'singular_name' => _x( $singcpost, $singcpost ),
        'add_new' => _x( 'Add New', $singcpost ),
        'add_new_item' => _x( 'Add New '.$singcpost.'', $singcpost ),
        'edit_item' => _x( 'Edit '.$singcpost.'', $singcpost ),
        'new_item' => _x( 'New '.$singcpost.'', $singcpost ),
        'view_item' => _x( 'View '.$singcpost.'', $singcpost ),
        'search_items' => _x( 'Search '.$plurcpost.'', $singcpost ),
        'not_found' => _x( 'No '.$plurcpost.' found', $singcpost ),
        'not_found_in_trash' => _x( 'No '.$plurcpost.' found in Trash', $singcpost ),
        'parent_item_colon' => _x( 'Parent '.$singcpost.':', $singcpost ),
        'menu_name' => _x( $plurcpost, $singcpost ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => $desccpost,
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category', 'post_tag', 'page-category', 'customtax1', 'customtax2', 'customtax3' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => $imgcpost,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( $singcpost, $args );
}
/* IF 1 END */
    /* IF 2 Start */
add_action( 'init', 'register_cpt_cpost_mdframework_2' );

function register_cpt_cpost_mdframework_2() {
    $singcpost = of_get_option('custom_posts_name_s_n2');
    $plurcpost = of_get_option('custom_posts_name_p_n2');
    $desccpost = of_get_option('custom_posts_name_d_n2');
    $imgcpost  = of_get_option('custom_posts_name_i_n2');
    $labels = array( 
        'name' => _x( $plurcpost, $singcpost ),
        'singular_name' => _x( $singcpost, $singcpost ),
        'add_new' => _x( 'Add New', $singcpost ),
        'add_new_item' => _x( 'Add New '.$singcpost.'', $singcpost ),
        'edit_item' => _x( 'Edit '.$singcpost.'', $singcpost ),
        'new_item' => _x( 'New '.$singcpost.'', $singcpost ),
        'view_item' => _x( 'View '.$singcpost.'', $singcpost ),
        'search_items' => _x( 'Search '.$plurcpost.'', $singcpost ),
        'not_found' => _x( 'No '.$plurcpost.' found', $singcpost ),
        'not_found_in_trash' => _x( 'No '.$plurcpost.' found in Trash', $singcpost ),
        'parent_item_colon' => _x( 'Parent '.$singcpost.':', $singcpost ),
        'menu_name' => _x( $plurcpost, $singcpost ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => $desccpost,
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category', 'post_tag', 'page-category', 'customtax1', 'customtax2', 'customtax3' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => $imgcpost,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( $singcpost, $args );
}
/* IF 2 END */
    /* IF 3 Start */    
add_action( 'init', 'register_cpt_cpost_mdframework_3' );

function register_cpt_cpost_mdframework_3() {
    $singcpost = of_get_option('custom_posts_name_s_n3');
    $plurcpost = of_get_option('custom_posts_name_p_n3');
    $desccpost = of_get_option('custom_posts_name_d_n3');
    $imgcpost  = of_get_option('custom_posts_name_i_n3');
    $labels = array( 
        'name' => _x( $plurcpost, $singcpost ),
        'singular_name' => _x( $singcpost, $singcpost ),
        'add_new' => _x( 'Add New', $singcpost ),
        'add_new_item' => _x( 'Add New '.$singcpost.'', $singcpost ),
        'edit_item' => _x( 'Edit '.$singcpost.'', $singcpost ),
        'new_item' => _x( 'New '.$singcpost.'', $singcpost ),
        'view_item' => _x( 'View '.$singcpost.'', $singcpost ),
        'search_items' => _x( 'Search '.$plurcpost.'', $singcpost ),
        'not_found' => _x( 'No '.$plurcpost.' found', $singcpost ),
        'not_found_in_trash' => _x( 'No '.$plurcpost.' found in Trash', $singcpost ),
        'parent_item_colon' => _x( 'Parent '.$singcpost.':', $singcpost ),
        'menu_name' => _x( $plurcpost, $singcpost ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => $desccpost,
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category', 'post_tag', 'page-category', 'customtax1', 'customtax2', 'customtax3' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => $imgcpost,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( $singcpost, $args );
}
/* IF 3 END */
    /* IF 4 Start */
add_action( 'init', 'register_cpt_cpost_mdframework_4' );

function register_cpt_cpost_mdframework_4() {
    $singcpost = of_get_option('custom_posts_name_s_n4');
    $plurcpost = of_get_option('custom_posts_name_p_n4');
    $desccpost = of_get_option('custom_posts_name_d_n4');
    $imgcpost  = of_get_option('custom_posts_name_i_n4');
    $labels = array( 
        'name' => _x( $plurcpost, $singcpost ),
        'singular_name' => _x( $singcpost, $singcpost ),
        'add_new' => _x( 'Add New', $singcpost ),
        'add_new_item' => _x( 'Add New '.$singcpost.'', $singcpost ),
        'edit_item' => _x( 'Edit '.$singcpost.'', $singcpost ),
        'new_item' => _x( 'New '.$singcpost.'', $singcpost ),
        'view_item' => _x( 'View '.$singcpost.'', $singcpost ),
        'search_items' => _x( 'Search '.$plurcpost.'', $singcpost ),
        'not_found' => _x( 'No '.$plurcpost.' found', $singcpost ),
        'not_found_in_trash' => _x( 'No '.$plurcpost.' found in Trash', $singcpost ),
        'parent_item_colon' => _x( 'Parent '.$singcpost.':', $singcpost ),
        'menu_name' => _x( $plurcpost, $singcpost ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => $desccpost,
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category', 'post_tag', 'page-category', 'customtax1', 'customtax2', 'customtax3' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => $imgcpost,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( $singcpost, $args );
}
/* IF 4 END */
    /* IF 5 Start */
add_action( 'init', 'register_cpt_cpost_mdframework_5' );

function register_cpt_cpost_mdframework_5() {
    $singcpost = of_get_option('custom_posts_name_s_n5');
    $plurcpost = of_get_option('custom_posts_name_p_n5');
    $desccpost = of_get_option('custom_posts_name_d_n5');
    $imgcpost  = of_get_option('custom_posts_name_i_n5');
    $labels = array( 
        'name' => _x( $plurcpost, $singcpost ),
        'singular_name' => _x( $singcpost, $singcpost ),
        'add_new' => _x( 'Add New', $singcpost ),
        'add_new_item' => _x( 'Add New '.$singcpost.'', $singcpost ),
        'edit_item' => _x( 'Edit '.$singcpost.'', $singcpost ),
        'new_item' => _x( 'New '.$singcpost.'', $singcpost ),
        'view_item' => _x( 'View '.$singcpost.'', $singcpost ),
        'search_items' => _x( 'Search '.$plurcpost.'', $singcpost ),
        'not_found' => _x( 'No '.$plurcpost.' found', $singcpost ),
        'not_found_in_trash' => _x( 'No '.$plurcpost.' found in Trash', $singcpost ),
        'parent_item_colon' => _x( 'Parent '.$singcpost.':', $singcpost ),
        'menu_name' => _x( $plurcpost, $singcpost ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => $desccpost,
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category', 'post_tag', 'page-category', 'customtax1', 'customtax2', 'customtax3' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => $imgcpost,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( $singcpost, $args );
}
/* IF 5 END */
    ;?>

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.