I am attempting to create a plugin that uses the setting API to add some options for my user. I currently create a new menu page and then create a setting section. This all works fine but then my callback function isn't being called. Can anyone see why?
function register_my_custom_menu_page() {
add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'menu-slug', 'sandbox_initialize_theme_options' );
}
add_action( 'admin_menu', 'register_my_custom_menu_page' );
function sandbox_initialize_theme_options() {
// First, we register a section. This is necessary since all future options must belong to one.
add_settings_section(
'general_settings_section', // ID used to identify this section and with which to register options
'Sandbox Options', // Title to be displayed on the administration page
'sandbox_general_options_callback', // Callback used to render the description of the section
'menu-slug' // Page on which to add this section of options
);
} // end sandbox_initialize_theme_options
function sandbox_general_options_callback() {
echo '<p>Select which areas of content you wish to display.</p>';
} // end sandbox_general_options_callback
Related
i have been making a plugin which makes it possible to show a schedule in wordpress, but until now i did not find a good way to show the page in wordress.
until now what i did was check if certain querystring variable was set.
but now i found a way to add virtual pages to wordpress:
https://coderwall.com/p/fwea7g/create-wordpress-virtual-page-on-the-fly
i renamed it to virtualClass and used it like this:
new \helpers\VirtualPage(array(
'slug' => '1002',
'post_title' => 'Fake Page Title',
'post content' => 'This is the fake page content'
));
to tryout, i went to the page: mywordpress.site?page_id=1002
this did work, but when i went to the backend in the menu builder, i wont see this page, i want it to be shown there so my users can add the page to their website.
Is there good tutorial on how to do this? is there a hook i can use to add a section to the menu builder?
Like in the image below: add it to either place of the red arrow
Instead of using a virtual page, you should create a page with your plugin's activation and then overwrite that page's content. You will also want to delete
the page on deactivation. In case you want to protect that page from user deletion, I have included that code as well, but it uses and exit; so it's a bit harsh.
function pluginxyz_activate(){
$title = 'Whatever You Want To Call It';
$new_id = wp_insert_post(array(
'post_title'=>$title,
'post_status'=>'publish',
'post_type'=>'page'
));
update_option('pluginxyz_page_id',$new_id);
}
function pluginxyz_deactivate(){
$old_id = get_option('pluginxyz_page_id');
if($old_id)wp_delete_post($old_id,true);
delete_option('pluginxyz_page_id');
}
function pluginxyz_protect_page($post_id){
$old_id = get_option('pluginxyz_page_id');
if($post_id == $old_id)exit('You can not delete this page');
}
function pluginxyz_use_content($content){
$old_id = get_option('pluginxyz_page_id');
global $post;
if($post->ID == $old_id){
//que up your content
} else {
return $content;
}
}
register_activation_hook(__FILE__,'pluginxyz_activate');
register_deactivation_hook( __FILE__, 'pluginxyz_deactivate');
add_action('wp_trash_post', 'pluginxyz_protect_page', 10, 1);
add_action('before_delete_post', 'pluginxyz_protect_page', 10, 1);
add_filter( 'the_content', 'pluginxyz_use_content' );
I am using this function to create menu option in wp dashboard
add_menu_page(
__('Advertisement Pages'),// the page title
__('Advertisement'),//menu title
'edit_themes',//capability
'a-advertise',//menu slug/handle this is what you need!!!
'display_all_ad',//callback function
'',//icon_url,
'15'//position
);
it is creating menu the name of Advertisement but also creating submenu with the same name.
try this
add_action( 'admin_menu', 'advertisement_menus' );
function advertisement_menus() {
add_menu_page( 'Advertisement Pages', 'Advertisement', 'edit_themes', 'advertisement_slug', 'display_all_ad', '', 15 );
}
//and your call back function
function display_all_ad(){
echo "this is call back";
}
I have created this custom plugin and after the activation it shows "settings" on the installed plugin.
I do not want to show my setting page LINK anywhere in the the admin panel menu but when user clicks the setting of the plugin (see the above screen shot) that should go to the plugin setting page. I understand that I have to do something on my second function "our_plugin_action_links" 's variable $settings_link
I just tried to link a single php file that placed on the admin folder. Then it goes to that php file when clicks. But I want to show the plugin setting page inside the admin panel same like other pages such as add post or **general setting page when click setting of the plugin but not showing the links or menus at the admin menu for the plugin setting.
How can I do that?
my plugin code
<?php
/*
Plugin Name: admin menu remover
Description: Remove the admin menus just by a single plugin installation
Version: 1.0
Author: Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
/* This function and action removes following menu item from the admin panel */
add_action( 'admin_menu', 'remove_links_menu' );
function remove_links_menu() {
remove_menu_page('index.php'); // Dashboard
//remove_menu_page('edit-comments.php'); // Comments
remove_menu_page('themes.php'); // Appearance
//remove_menu_page('plugins.php'); // Plugins
//remove_menu_page('tools.php'); // Tools
//remove_menu_page('options-general.php'); // Settings
//remove_menu_page('users.php'); // Users
}
/* This function and filter append "setting" immediately after the "admin menu remover" plugin activation */
add_filter('plugin_action_links', 'our_plugin_action_links', 10, 2);
function our_plugin_action_links($links, $file) {
static $this_plugin;
if (!$this_plugin) {
$this_plugin = plugin_basename(__FILE__);
}
// check to make sure we are on the correct plugin
if ($file == $this_plugin) {
// the anchor tag and href to the URL we want. For a "Settings" link, this needs to be the url of your settings page
$settings_link = 'Settings';
// add the link to the list
array_unshift($links, $settings_link);
}
return $links;
}
?>
First off, from a user's perspective I wouldn't recommend doing this. If your plugin warrants a settings page then put it where settings below. If you don't trust your users then don't allow the settings OR use capabilities to restrict your menu to only certain users.
However, if you've got a specific need for this then the easiest way is to just register a normal options page using add_options_page and then manually rip your menu out of the global array. This method ensures that permissions are accounted for correctly and is pretty safe.
Also, instead of plugin_action_links you can use the filter that is specific to your plugin via 'plugin_action_links_' . plugin_basename( __FILE__ ) which is what the code below does. You'll need to change the constant to something more specific to your code (or switch to a global variable or just use strings). See the code comments for more details.
//We'll key on the slug for the settings page so set it here so it can be used in various places
define( 'MY_PLUGIN_SLUG', 'my-plugin-slug' );
//Register a callback for our specific plugin's actions
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'my_plugin_action_links' );
function my_plugin_action_links( $links )
{
$links[] = 'Settings';
return $links;
}
//Create a normal admin menu
add_action( 'admin_menu', 'register_settings' );
function register_settings()
{
add_options_page( 'My Plugin Settings', 'My Plugin Settings', 'manage_options', MY_PLUGIN_SLUG, 'my_plugin_settings_page' );
//We just want to URL to be valid so now we're going to remove the item from the menu
//The code below walks the global menu and removes our specific item by its slug
global $submenu;
if( array_key_exists( 'options-general.php' , $submenu ) )
{
foreach( $submenu['options-general.php'] as $k => $v )
{
if( MY_PLUGIN_SLUG === $v[2] )
{
unset( $submenu['options-general.php'][$k] );
}
}
}
}
//This is our plugins settings page
function my_plugin_settings_page()
{
echo 'Hello from my plugin!';
}
You can try the generator on this site.
http://wpsettingsapi.jeroensormani.com/
What it does is you make a callback function where you set your options. So not a seperate file but just your functions.php or plugin file.
Or checkout the codex http://codex.wordpress.org/Creating_Options_Pages
I am currently working on a custom theme's 'theme options' panel for a client that plans to distribute the theme for free after I am finished.
The client would like the theme options panel hidden/locked until the user has signed up for a newsletter. I have never done this before and was wondering if anybody has come across a tutorial or can point me in the right direction.
I've tried 'Googling' this topic for the past hour, but I haven't found anything useful.
Thanks!
How about check user has been signed up or not before adding new menu for your theme options?
maybe look like this
add_action( 'admin_menu', 'reg_my_theme_options' );
function reg_my_theme_options(){
if(!is_user_signup_newsletter())
return;
add_menu_page( 'Theme Options', 'Theme options', 'manage_options', 'theme-options', 'my_theme_options' );
}
function my_theme_options(){
// here your theme options page
echo "Admin Page Test";
}
function is_user_signup_newsletter(){
// here your signup newsletter logic
// maybe stored on options table
$nlt_signup_status = get_option( 'signed_up_newsletter', 'no' );
if($nlt_signup_status == 'yes'){
return true;
} else {
return false;
}
}
Insert HTML Input to a themes template file?
My question is this:
Currently with my code, I can have whatever I've set in my custom settings page appear on
the end of every post using, of course, a WordPress filter.
The only thing is, I don't want what I input to go anywhere within a post. What I am trying to
achieve, is to have my input injected into the current themes home.php template file, within a <div></div> tag that's contained within that template file. The <div> in question has an ID attached, which is <div id="category-inner">, so is there any way use it's ID to target it in my plugin?
I've successfully managed to do it, by editing the actual home.php template file and inserting a bit of php directly there to show the user input, but that totally goes against what I'm trying to do, which is to not have to edit the source code of the template file and only have to use my plugin to insert the users inputted text in that specific location (the <div> mentioned above).
My plugin is only ever going to be adding user input in one place on the site, and it will never change. And the place it will always go in, is where I mentioned above.
Below is my plugin code:
<?php
/*/
Plugin Name: Custom Text Adder
Plugin URI: N/A
Description: Demonstrates how rubbish I am at pretty much everything I want to do
Version: 101
Author: JD
Author URI: N/A
/*/
// insert custom plugin settings menu
add_action('admin_menu', 'custom_create_menu');
add_filter('the_content', 'customhead_the_content');
function customhead_the_content($content) {
$output = $content;
$output .= '<div id="category-inner">';
$output .= get_option('post_text');
$output .= '</div>';
return $output;
}
// Add Font-Size to WYSIWYG Editor
function wp_editor_fontsize_filter( $options ) {
array_shift( $options );
array_unshift( $options, 'fontsizeselect');
array_unshift( $options, 'formatselect');
return $options;
}
add_filter('mce_buttons_2', 'wp_editor_fontsize_filter');
// Create Custom Menu
function custom_create_menu() {
//create new top-level menu
add_menu_page('Custom Plugin Settings', 'Custom Settings', 'administrator', __FILE__, 'custom_settings_page',plugins_url('/img/icon.png', __FILE__));
//call register settings function
add_action( 'admin_init', 'register_mysettings' );
}
// Register our settings
function register_mysettings() {
register_setting( 'custom-settings-group', 'new_option_name' );
register_setting( 'custom-settings-group', 'some_other_option' );
register_setting( 'custom-settings-group', 'option_etc' );
register_setting( 'custom-settings-group', 'font_size' );
register_setting( 'custom-settings-group', 'post_text' );
}
function custom_settings_page() {
?>
<!-- Custom Settings Page Container -->
<div class="wrap">
<h2>Custom Text</h2>
<form method="post" action="options.php">
<?php settings_fields( 'custom-settings-group' ); ?>
<table class="form-table">
<?php /* Bring the editor onto the page */
wp_editor( '', 'post_text', $settings = array() );
// 4.
// Custom buttons for the editor.
// This is a list separated with a comma after each feature
// eg. link, unlink, bold, ...
$settings = array(
'textarea_name' => 'post_text',
'media_buttons' => true,
'tinymce' => array(
'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,' .
'bullist,blockquote,|,justifyleft,justifycenter' .
',justifyright,justifyfull,|,link,unlink,|' .
',spellchecker,wp_fullscreen,wp_adv'
)
);
submit_button( 'Save everything', 'primary', 'submit' ); ?>
</table>
</form>
</div>
<?php }; ?>
Here is the screenshot which will be able to explain this in the most concise way possible:
http://i.imgur.com/hiEjEsA.jpg
Again, any and all help is hugely appreciated and will hopefully stop my brain from hurting!
You all rock,
Casey. :)
I'm not sure if you want this to be a pure PHP solution, but since you have the ID of the div you could target it with jQuery and insert the text into the div on page load (or form submit) with something like this:
$(document).ready(function() {
var userText = $('#input-id').val();
$('#category-inner').html(userText);
})