Ask your WordPress questions! Pay money and get answers fast! (more info)

Plugin Development Simple Function; Display in Plugin, Save WordPress

  • SOLVED

I'm building a WordPress plugin. The plugin has a menu page and is divided into multiple submenu pages. My code is included below.

Currently, I am working on schema.php. I would like to add a dropdown box which allows users to show or not show a code which generates post views; the array code below could be used to achieve this this.

Here's the thing - I don't know how to:
1. Create a <em>functions file</em> for schema.php which includes the array for the dropdown box and allows it to be saved
2. Display the dropdown box on the frontend (in schema.php) of the plugin, displaying the name, description, and dropdown box

Please provide me with a code for the plugin's functions file and a code for schema.php which allow this to be done.

array( "name" => "Display post views?",
"desc" => "Choose whether or not to display post views.",
"id" => $shortname."_post_views",
"type" => "select",
"options" => array("Yes", "No"),
"std" => "Yes"),


<strong>Plugin code thus far</strong>

// Don't mess with this
if (!defined('WP_CONTENT_URL'))
define('WP_CONTENT_URL', get_option('siteurl').'/wp-content');
if (!defined('WP_CONTENT_DIR'))
define('WP_CONTENT_DIR', ABSPATH.'wp-content');
if (!defined('WP_PLUGIN_URL'))
define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins');
if (!defined('WP_PLUGIN_DIR'))
define('WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins');

// Add menus and submenus
function plugin_panel(){
add_menu_page( 'Plugin page title', 'Dashboard', 'manage_options', 'theme-options', 'wfunc');
add_submenu_page( 'theme-options', 'Social page title', 'Social', 'manage_options', 'wlseo-social', 'wfunc_social');
add_submenu_page( 'theme-options', 'Schema page', 'Schema', 'manage_options', 'wlseo-schema', 'wfunc_schema');
add_submenu_page( 'theme-options', 'Edit files page', 'Edit Files', 'manage_options', 'wlseo-files', 'wfunc_files');
}
add_action('admin_menu', 'plugin_panel');

// Grab titles and add them to the pages
function wfunc(){
echo '<div class="wrap"><div id="icon-options-general" class="icon32"><br></div>
<h2>Dashboard</h2></div>';
}
function wfunc_social(){
include(WP_PLUGIN_DIR.'/wlseo/social.php');
}
function wfunc_schema(){
include(WP_PLUGIN_DIR.'/wlseo/schema.php');
}
function wfunc_files(){
include(WP_PLUGIN_DIR.'/wlseo/files.php');
}

Answers (2)

2014-01-28

Bob answers:

Please download this tiny plugin [[LINK href="http://wp-lovers.com/wpquestions/wlseo.zip"]]http://wp-lovers.com/wpquestions/wlseo.zip[[/LINK]]

I know that you are creating multiple page for your plugin, but instead of creating multiple page you can create settings tabs.
Above plugin is such example specially created for you.

this will be lot easy for your plugin user to navigate in different sections of your settings with this type of settings page.

In above plugin line no. 27 to 31 define different sections of your plugin.
And line no. 295 to 407 shows examples different types of input fields you can use for settings. like textbox, select, textarea, checkbox etc.

or let me know if you have any questions about above plugin.

use it if you wish or let me know I will try find something different.


siouxfan45 comments:

Thanks @Bhavesh, great code! I'll have to use your services again in the future.

2014-01-28

Hariprasad Vijayan answers:

Hello,

I am not clear about your requirement. I think you can do it something like this

<?php
if(!empty($_POST) && $_POST['hid_id'] == 'schema' )
{
$schema = array();
$schema['name'] = $_POST['txt_name'];
$schema['des'] = $_POST['txt_des'];
$schema['view'] = $_POST['sel_view'];
$value = serialize($schema);
update_option( 'schema', $value );
}
$option = get_option( 'schema' );
$schema = unserialize($option); ?>
<form name="schema" method="post" action="">
<input name="txt_name" placeholder="Name" value="<?php if(!empty($schema['name'])){ echo $schema['name']; } ?>" />
<input name="txt_des" placeholder="Description" value="<?php if(!empty($schema['des'])){ echo $schema['des']; } ?>" />
<select name="sel_view">
<option value="">View</option>
<option value="asc" <?php if(!empty($schema['view']) && $schema['view'] == 'asc' ){ echo 'selected'; } ?>>ASC</option>
<option value="desc" <?php if(!empty($schema['view']) && $schema['view'] == 'desc' ){ echo 'selected'; } ?>>DESC</option>
</select>
<input type="hidden" name="hid_id" value="schema" />
<input type="submit" value="Save" />
</form>

I think you can do it like this. You can place the code in you your schema.php and change it as per your need. Let me know if you need any help.