• bjusters

    (@bjusters)


    I`m creating a website in which a custom post type is created for each created category. This works great, however i wish to add a settings page to each custom post type section, holding unique values for each post type.

    i have it set up like this:

    add_action('admin_menu' , 'add_site_settings');
    function add_site_settings() {
    			$tax_terms = get_terms( 'category', 'orderby=count&hide_empty=0' );
    			$count = count($tax_terms);
    			 if ( $count > 0 ){
    				foreach ( $tax_terms as $term ) {
    				if($term->slug!='uncategorized' && $term->slug!='' ) {
    				 	add_submenu_page('edit.php?post_type='.$term->name, 'Theme settings for '.$term->name, 'Theme settings', 'edit_posts', $term->name, 'plugin_options_page');
    				}
    
    			}
    		 }
    	}
    
    	function plugin_options_page() {
    		?>
    	<div>
    	<h2>My custom plugin</h2>
    	Options relating to the Custom Plugin.
    	<form action="options.php" method="post">
    
    	<?php settings_fields('plugin_options'); ?>
    	<?php do_settings_sections('plugin'); ?>
    
    	<input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
    	</form></div>
    	<?php
    	}
    
    	add_action('admin_init', 'plugin_admin_init');
    	function plugin_admin_init(){
    		register_setting( 'plugin_options', 'plugin_options', 'plugin_options_validate' );
    		add_settings_section('plugin_main', 'Main Settings', 'plugin_section_text', 'plugin');
    		add_settings_field('plugin_text_string', 'Plugin Text Input', 'plugin_setting_string', 'plugin', 'plugin_main');
    	}
    
    	function plugin_setting_string() {
    		$options = get_option('plugin_options');
    		echo "<input id='plugin_text_string' name='plugin_options[text_string]' size='40' type='text' value='{$options['text_string']}' />";
    	} 	
    
     // validate our options
    	function plugin_options_validate($input) {
    		$newinput['text_string'] = trim($input['text_string']);
    		/*if(!preg_match('/^[a-z0-9]{32}$/i', $newinput['text_string'])) {
    		$newinput['text_string'] = '';
    		}*/
    		return $newinput;
    	}

    This creates a settings page under each custom post type i creates, however, it`s the same for each custom post type, so the value i enter in CPT1 will also be shown in CPT2.

    Its needs to be dynamic, i cant create all functions in advance (that would be a solution) so each time a category is added, a new CPT needs to be added with settings page.

    Problem is, i can`t pass variables to plugin_options_page, i tried to trick it with a $_GET variable but no luck. Maybe a global would help, but how and will it be safe?

    Any briljant ideas?

Viewing 1 replies (of 1 total)
  • Thread Starter bjusters

    (@bjusters)

    This is the script to auto create the CPTypes, might be uselfull for someone:

    [51 lines of code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

Viewing 1 replies (of 1 total)
  • The topic ‘Auto create settings page for each custom post type’ is closed to new replies.