Blank output value from Wordress custom admin settings field - wordpress

I've added the example cod shown below. This works like a charm. But I am unable to output the saved values on my page.
Using:
get_option( 'title' )
appears to have no value, despite the new admin section properly saving a value for it.
I've also tried get_option( 'title','does not work' ) and confirmed that 'title' is empty, as it's outputting default value of 'does not work'
Here is the code I've added to the function file. All appears fine in admin and saves values as expected. (this is example #2 here: https://codex.wordpress.org/Creating_Options_Pages)
class MySettingsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'My Settings',
'manage_options',
'my-setting-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'my_option_name' );
?>
<div class="wrap">
<h1>My Settings</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_option_group' );
do_settings_sections( 'my-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_option_group', // Option group
'my_option_name', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'My Custom Settings', // Title
array( $this, 'print_section_info' ), // Callback
'my-setting-admin' // Page
);
add_settings_field(
'id_number', // ID
'ID Number', // Title
array( $this, 'id_number_callback' ), // Callback
'my-setting-admin', // Page
'setting_section_id' // Section
);
add_settings_field(
'title',
'Title',
array( $this, 'title_callback' ),
'my-setting-admin',
'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* #param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['id_number'] ) )
$new_input['id_number'] = absint( $input['id_number'] );
if( isset( $input['title'] ) )
$new_input['title'] = sanitize_text_field( $input['title'] );
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter your settings below:';
}
/**
* Get the settings option array and print one of its values
*/
public function id_number_callback()
{
printf(
'<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />',
isset( $this->options['id_number'] ) ? esc_attr( $this->options['id_number']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function title_callback()
{
printf(
'<input type="text" id="title" name="my_option_name[title]" value="%s" />',
isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();

Your options are saved under a single key my_option_name, so you would access the individual options like:
$my_options = get_option( 'my_option_name' );
echo $my_options['title'];
echo $my_options['id_number'];

Related

WordPress Settings validation messages are shown twice

I'm adding a pretty basic (I feel) implementation of settings page validation for my WordPress plugin and it does work, but the error messages are shown twice. I stepped through my code and the calls to add_settings_error are only executed once.
<?php
class Example_plugin_Settings {
private $example_plugin_settings_options;
private $settings_options_name;
public function __construct() {
add_action( 'admin_menu', array( $this, 'example_plugin_settings_add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'example_plugin_settings_page_init' ) );
$this->settings_options_name = 'example_plugin_options';
}
public function example_plugin_settings_add_plugin_page() {
add_options_page(
'Example-Plugin Connector Settings', // page_title
'Example-Plugin', // menu_title
'manage_options', // capability
'example-plugin-connector-settings', // menu_slug
array( $this, 'example_plugin_settings_create_admin_page' ) // function
);
}
public function example_plugin_settings_create_admin_page() {
$this->example_plugin_settings_options = get_option( 'example_plugin_options' ); ?>
<div class="wrap">
<h2>Example-Plugin Connector Settings</h2>
<p>Set up additional portals/currencies to be used with WooCommerce Currency Switcher (WOOCS). Enter a comma-delimited list of portals, then the corresponding comma-delimited list of currencies that those portals support.</p>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'example_plugin_settings_option_group' );
do_settings_sections( 'example-plugin-connector-settings-admin' );
submit_button();
?>
</form>
</div>
<?php }
public function example_plugin_settings_page_init() {
register_setting(
'example_plugin_settings_option_group', // option_group
$this->settings_options_name, // option_name
array( $this, 'example_plugin_settings_sanitize' ) // sanitize_callback
);
add_settings_section(
'example_plugin_settings_setting_section', // id
'Settings', // title
array( $this, 'example_plugin_settings_section_info' ), // callback
'example-plugin-connector-settings-admin' // page
);
add_settings_field(
'portals', // id
'Portals', // title
array( $this, 'portals_callback' ), // callback
'example-plugin-connector-settings-admin', // page
'example_plugin_settings_setting_section' // section
);
add_settings_field(
'currencies', // id
'Currencies', // title
array( $this, 'currencies_callback' ), // callback
'example-plugin-connector-settings-admin', // page
'example_plugin_settings_setting_section' // section
);
}
public function example_plugin_settings_sanitize($input) {
$sanitary_values = array();
if ( isset( $input['portals'] ) ) {
if ( '' == $input['portals'] ) {
$input['portals'] = '';
add_settings_error(
$this->settings_options_name,
'portals',
'Portals is a required field.',
'error'
);
} else {
$sanitary_values['portals'] = sanitize_text_field( trim( $input['portals'] ) );
}
}
if ( isset( $input['currencies'] ) ) {
if ( '' == $input['currencies'] ) {
$input['currencies'] = '';
add_settings_error(
$this->settings_options_name,
'currencies',
'Currencies is a required field.',
'error'
);
} else {
$sanitary_values['currencies'] = sanitize_text_field( trim( $input['currencies'] ) );
}
}
return $sanitary_values;
}
public function example_plugin_settings_section_info() {
}
public function portals_callback() {
printf(
'<input class="regular-text" type="text" name="example_plugin_options[portals]" id="portals" value="%s">',
isset( $this->example_plugin_settings_options['portals'] ) ? esc_attr( $this->example_plugin_settings_options['portals']) : ''
);
}
public function currencies_callback() {
printf(
'<input class="regular-text" type="text" name="example_plugin_options[currencies]" id="currencies" value="%s">',
isset( $this->example_plugin_settings_options['currencies'] ) ? esc_attr( $this->example_plugin_settings_options['currencies']) : ''
);
}
}
if ( is_admin() )
$example_plugin_settings = new Example_plugin_Settings();
It seems the problem is the line <?php settings_errors(); ?>. I got the base code from the WordPress Options Page Generator but maybe the code is outdated and that line is now redundant with newer versions of WordPress? I'm not sure.

how to get wp_editor to save data in plugin admin page

I'm trying to use plugin settings api to add wp_editor, but the text/html is not saving.
// add the admin settings and such
add_action('admin_init', 'wp_myplugin_admin_init');
function wp_myplugin_admin_init(){
register_setting( 'wp_myplugin_settings', 'wp_myplugin_settings', 'wp_myplugin_settings_validate');
add_settings_field('wp_myplugin_user_custom_text', __('Enter your message','WP-wp_myplugin'), 'wp_myplugin_user_custom_text', 'wp_myplugin', 'wp_myplugin_main');
function wp_myplugin_user_custom_text() {
$options = get_option('wp_myplugin_settings');
$settings = array('textarea_rows' => 5,'textarea_name' => 'user_cutom _text_msg');
wp_editor( $options['user_custom_text'],'user_custom_text', $settings );}
// validate
function wp_myplugin_settings_validate() {
$options = get_option('wp_myplugin_settings');
$user_custom_text = $input['user_custom_text'];
if ( empty($user_custom_text) ){
$options['user_custom_text'] = $user_custom_text;
}else{
$options['user_custom_text'] = __('Enter your own text','WP-wp_myplugin');// as set when the plugin activated
I used the $input['user_custom_text'];
all I needed was $_POST['user_custom_text'];
also to get the media to work need wordpress Sanitize:
<?php wp_kses_post( $data ); ?>
http://codex.wordpress.org/Function_Reference/wp_kses_post
Incase there are anyone who followed the steps on the Creating Options Page on the Wordpress Codex and finding a hard time saving their input, you can use the below code to make sure the wp_editor saves your input.
<?php
class MySettingsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'My Settings',
'manage_options',
'my-setting-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'my_option_name' );
?>
<div class="wrap">
<h1>My Settings</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_option_group' );
do_settings_sections( 'my-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_option_group', // Option group
'my_option_name', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'My Custom Settings', // Title
array( $this, 'print_section_info' ), // Callback
'my-setting-admin' // Page
);
add_settings_field(
'my_content', //This ID of the field you want to use wp_editor for
'Write Your Content Here',
array( $this, 'my_content_callback' ),
'my-setting-admin',
'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* #param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['my_content'] ) )
$new_input['my_content'] = wp_kses_post( $input['my_content'] );
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter your settings below:';
}
/**
* Get the settings option array and print one of its values
*/
public function my_content_callback()
{
$args = array (
'media_buttons' => false,
'textarea_rows' => '10',
'textarea_name' => 'my_option_name[my_content]'
);
$options = get_option('my_option_name');
wp_editor( $options['my_content'], 'my_content', $args );
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();

How to Add Tabs for WordPress Plugin Settings Page?

I have created a settings page for my wordpress plugin as described in codex (https://codex.wordpress.org/Creating_Options_Pages) #Example 2. My question is how to add tabbed options (tab interface) for WordPress Plugin Settings page for #Example 2 on WP codex?
I have created a Setting page for my plugin with 'Composer Autoload'
but for now, I am sharing just simple class to show how
my plugin has the setting page two tabs
'PDF Setting Tab'
'Email Setting Tab'
if ( ! class_exists( 'Plugin_Name_Settings_Page' ) ) {
class Plugin_Name_Settings_Page {
/* Admin Parent Menu item - properties */
private $page_title;
private $menu_title;
private $capability;
private $menu_slug;
private $function;
private $icon_url;
private $position;
public $page_menu;
public function __construct() {
$this->page_title = 'Plugin_Name Settings';
$this->menu_title = 'Plugin_Name Settings';
$this->capability = 'manage_options'; //'edit_pages';
$this->menu_slug = 'Plugin_Name-settings-page'; //'Plugin_Name-admin-dashboard';
$this->function = array( $this, 'page' );
$this->icon_url = 'dashicons-cart';
$this->position = 24;
// Create Admin Page & Menu
add_action( 'admin_menu', array( $this, 'CreatePageMenu' ), 10 );
add_action( 'Plugin_Name_settings_page_created', array( $this, 'Plugin_Name_pdf_settings_init' ), 10 );
}
public function CreatePageMenu() {
$this->page_menu = add_menu_page(
$this->page_title,
$this->menu_title,
$this->capability,
$this->menu_slug,
$this->function,
$this->icon_url,
$this->position,
);
do_action( 'Plugin_Name_settings_page_created' );
}
public function page() {
?>
<div class="wrap">
<!-- Create a header in the default WordPress 'wrap' container -->
<div id="icon-themes" class="icon32"></div>
<h2>Plugin_Name Settings</h2>
<?php settings_errors(); ?>
<?php
if ( isset( $_GET ) ) {
$active_tab = $_GET['tab'];
}
?>
<h2 class="nav-tab-wrapper">
PDF Settings
Email Settings
</h2>
<form method="post" action="options.php">
<?php
if ( $active_tab == 'pdf_settings' ) {
// settings options group name passed
settings_fields( 'Plugin_Name_pdf_settings_options_group' );
// settings options name passed
do_settings_sections( 'Plugin_Name_pdf_settings_options' );
submit_button();
} elseif ( $active_tab == 'email_settings' ) {
// settings options group name passed
settings_fields( 'Plugin_Name_email_settings_options_group' );
// settings options name passed
do_settings_sections( 'Plugin_Name_email_settings_options' );
submit_button();
}
?>
</form>
</div><!-- /.wrap -->
<?php
}
/***** Start - Tab 01 - PDF Settings Script ***/
public function plugin_name_pdf_settings_init() {
register_setting(
'plugin_name_pdf_settings_options_group', // option_group
'plugin_name_pdf_settings_options', // option_name
array( $this, 'plugin_name_pdf_settings_options_sanitize' ), // sanitize_callback
);
add_settings_section(
'plugin_name_pdf_settings_options_section', // id
'plugin_name PDF Settings', // title
array( $this, 'plugin_name_pdf_settings_options_section_info' ), // callback
//'plugin_name-admin-dashboard', //$_GET['page'], // page
'plugin_name_pdf_settings_options'
);
add_settings_field(
'plugin_name_enrollment_agent_name_option', // id
'Enrollment Agent Name', // title
array( $this, 'plugin_name_enrollment_agent_name_option_callback' ), // callback
'plugin_name_pdf_settings_options', // page
'plugin_name_pdf_settings_options_section' // section
);
add_settings_field(
'plugin_name_enrollment_agent_cert_option', // id
'Enrollment Agent Cert #', // title
array( $this, 'plugin_name_enrollment_agent_cert_option_callback' ), // callback
'plugin_name_pdf_settings_options', // page
'plugin_name_pdf_settings_options_section' // section
);
add_settings_field(
'plugin_name_enrollment_accepted_by_option', // id
'Enrollment Accepted By', // title
array( $this, 'plugin_name_enrollment_accepted_by_option_callback' ), // callback
'plugin_name_pdf_settings_options', // page
'plugin_name_pdf_settings_options_section' // section
);
}
public function plugin_name_pdf_settings_options_section_info() {
echo 'plugin_name PDF Settings, admin can set fix daat for PDF.';
}
public function plugin_name_pdf_settings_options_sanitize( $input ) {
$sanitary_values = array();
if ( isset( $input['plugin_name_enrollment_agent_name_option'] ) ) {
$sanitary_values['plugin_name_enrollment_agent_name_option'] = $input['plugin_name_enrollment_agent_name_option'];
}
if ( isset( $input['plugin_name_enrollment_agent_cert_option'] ) ) {
$sanitary_values['plugin_name_enrollment_agent_cert_option'] = $input['plugin_name_enrollment_agent_cert_option'];
}
if ( isset( $input['plugin_name_enrollment_accepted_by_option'] ) ) {
$sanitary_values['plugin_name_enrollment_accepted_by_option'] = $input['plugin_name_enrollment_accepted_by_option'];
}
return $sanitary_values;
}
function plugin_name_enrollment_agent_name_option_callback() {
$plugin_name_pdf_option_agent_name_value = ( isset($this->plugin_name_pdf_settings_options['plugin_name_enrollment_agent_name_option'] ) ) ? $this->plugin_name_pdf_settings_options['plugin_name_enrollment_agent_name_option']: '';
?>
<input type="text" name="plugin_name_pdf_settings_options[plugin_name_enrollment_agent_name_option]" value="<?php echo $plugin_name_pdf_option_agent_name_value; ?>">
<?php
}
function plugin_name_enrollment_agent_cert_option_callback() {
$plugin_name_pdf_option_agent_cert_value = ( isset($this->plugin_name_pdf_settings_options['plugin_name_enrollment_agent_cert_option'] ) ) ? $this->plugin_name_pdf_settings_options['plugin_name_enrollment_agent_cert_option']: '';
?>
<input type="text" name="plugin_name_pdf_settings_options[plugin_name_enrollment_agent_cert_option]" value="<?php echo $plugin_name_pdf_option_agent_cert_value; ?>">
<?php
}
function plugin_name_enrollment_accepted_by_option_callback() {
$plugin_name_pdf_option_accepted_by_value = ( isset($this->plugin_name_pdf_settings_options['plugin_name_enrollment_accepted_by_option'] ) ) ? $this->plugin_name_pdf_settings_options['plugin_name_enrollment_accepted_by_option']: '';
?>
<input type="text" name="plugin_name_pdf_settings_options[plugin_name_enrollment_accepted_by_option]" value="<?php echo $plugin_name_pdf_option_accepted_by_value; ?>">
<?php
}
/*** End - Tab 01 - PDF Settings Script ***/
/**** STart - Tab 02 - Email Settings Script ***/
public function plugin_name_email_settings_init() {
register_setting(
'plugin_name_email_settings_options_group', // option_group
'plugin_name_email_settings_options', // option_name
array( $this, 'plugin_name_email_settings_options_sanitize' ), // sanitize_callback
);
add_settings_section(
'plugin_name_email_settings_options_section', // id
'plugin_name email Settings', // title
array( $this, 'plugin_name_email_settings_options_section_info' ), // callback
//'plugin_name-admin-dashboard', //$_GET['page'], // page
'plugin_name_email_settings_options'
);
add_settings_field(
'plugin_name_teacher_name_option', // id
'Enrollment Agent Name', // title
array( $this, 'plugin_name_teacher_name_option_callback' ), // callback
'plugin_name_email_settings_options', // page
'plugin_name_email_settings_options_section' // section
);
add_settings_field(
'plugin_name_teacher_cert_option', // id
'Enrollment Agent Cert #', // title
array( $this, 'plugin_name_teacher_cert_option_callback' ), // callback
'plugin_name_email_settings_options', // page
'plugin_name_email_settings_options_section' // section
);
add_settings_field(
'plugin_name_room_number_option', // id
'Enrollment Accepted By', // title
array( $this, 'plugin_name_room_number_option_callback' ), // callback
'plugin_name_email_settings_options', // page
'plugin_name_email_settings_options_section' // section
);
}
public function plugin_name_email_settings_options_section_info() {
echo 'plugin_name email Settings, admin can set fix daat for email.';
}
public function plugin_name_email_settings_options_sanitize( $input ) {
$sanitary_values = array();
if ( isset( $input['plugin_name_teacher_name_option'] ) ) {
$sanitary_values['plugin_name_teacher_name_option'] = $input['plugin_name_teacher_name_option'];
}
if ( isset( $input['plugin_name_teacher_cert_option'] ) ) {
$sanitary_values['plugin_name_teacher_cert_option'] = $input['plugin_name_teacher_cert_option'];
}
if ( isset( $input['plugin_name_room_number_option'] ) ) {
$sanitary_values['plugin_name_room_number_option'] = $input['plugin_name_room_number_option'];
}
return $sanitary_values;
}
function plugin_name_teacher_name_option_callback() {
$plugin_name_email_option_agent_name_value = ( isset($this->plugin_name_email_settings_options['plugin_name_teacher_name_option'] ) ) ? $this->plugin_name_email_settings_options['plugin_name_teacher_name_option']: '';
?>
<input type="text" name="plugin_name_email_settings_options[plugin_name_teacher_name_option]" value="<?php echo $plugin_name_email_option_agent_name_value; ?>">
<?php
}
function plugin_name_teacher_cert_option_callback() {
$plugin_name_email_option_agent_cert_value = ( isset($this->plugin_name_email_settings_options['plugin_name_teacher_cert_option'] ) ) ? $this->plugin_name_email_settings_options['plugin_name_teacher_cert_option']: '';
?>
<input type="text" name="plugin_name_email_settings_options[plugin_name_teacher_cert_option]" value="<?php echo $plugin_name_email_option_agent_cert_value; ?>">
<?php
}
function plugin_name_room_number_option_callback() {
$plugin_name_email_option_accepted_by_value = ( isset($this->plugin_name_email_settings_options['plugin_name_room_number_option'] ) ) ? $this->plugin_name_email_settings_options['plugin_name_room_number_option']: '';
?>
<input type="text" name="plugin_name_email_settings_options[plugin_name_room_number_option]" value="<?php echo $plugin_name_email_option_accepted_by_value; ?>">
<?php
}
/**** End - Tab 02 - Email Settings Script *****/
}
// Class Instence / object call for execution on requiring this page
new Plugin_Name_Settings_Page();
}
I found this tutorial online. Hope this helps.
http://plugin.michael-simpson.com/?page_id=47

How do you create a basic Wordpress admin pointer?

I have been looking around for quite awhile now and all I have found are tutorials from 3-4 years ago that explain how to do a pointer tour. All I want to do is add a pointer that pops up when someone activates my plugin so that I can notify them of a new menu option where they will go to view my plugin settings. Any help would be greatly appreciated!
Pointers in WP need 3 components:
1: wp-pointer css file
2: wp-pointer JS file
3: A JavaScript snippet
To 1 and 2
include them simply with:
wp_enqueue_style( 'wp-pointer' );
and
wp_enqueue_script( 'wp-pointer' );
The JS code:
<script type="text/javascript">
(function($){
var options = {"content":"<h3>Personal Data and Privacy<\/h3><h4>Personal Data Export and Erasure<\/h4><p>New <strong>Tools<\/strong> have been added to help you with personal data export and erasure requests.<\/p><h4>Privacy Policy<\/h4><p>Create or select your site’s privacy policy page under <strong>Settings > Privacy<\/strong> to keep your users informed and aware.<\/p>","position":{"edge":"left","align":"bottom"},"pointerClass":"wp-pointer arrow-bottom","pointerWidth":420}, setup;
if ( ! options )
return;
options = $.extend( options, {
close: function() {
$.post( ajaxurl, {
pointer: 'wp500_isrc_pointer',
action: 'dismiss-wp-pointer'
});
}
});
setup = function() {
$('#menu-settings').first().pointer( options ).pointer('open');
};
if ( options.position && options.position.defer_loading )
$(window).bind( 'load.wp-pointers', setup );
else
$(document).ready( setup );
})( jQuery );
</script>
Of Course you need to wrap all them in a php file to check the user capabilities and check the dismiss from the users meta.
I have copied the WP pointer class in wp-admin/includes/class-wp-internal-pointers and made a custom one from it.
Here the complete code which i can call it with an action hook like:
add_action( 'admin_enqueue_scripts', array( 'isrc_Internal_Pointers', 'enqueue_scripts') );
add_action( 'user_register',array( 'isrc_Internal_Pointers', 'dismiss_pointers_for_new_users' ) );
The Full PHP file (include it in your code and call the 2 actions):
<?php
/**
* Administration API: WP_Internal_Pointers class
*
* #package WordPress
* #subpackage Administration
* #since 4.4.0
*/
/**
* Core class used to implement an internal admin pointers API.
*
* #since 3.3.0
*/
final class isrc_Internal_Pointers {
/**
* Initializes the new feature pointers.
*
* #since 3.3.0
*
* All pointers can be disabled using the following:
* remove_action( 'admin_enqueue_scripts', array( 'WP_Internal_Pointers', 'enqueue_scripts' ) );
*
* Individual pointers (e.g. wp390_widgets) can be disabled using the following:
* remove_action( 'admin_print_footer_scripts', array( 'WP_Internal_Pointers', 'pointer_wp390_widgets' ) );
*
* #static
*
* #param string $hook_suffix The current admin page.
*/
public static function enqueue_scripts( $hook_suffix ) {
/*
* Register feature pointers
*
* Format:
* array(
* hook_suffix => pointer callback
* )
*
* Example:
* array(
* 'themes.php' => 'wp390_widgets'
* )
*/
$registered_pointers = array(
'index.php' => 'wp500_isrc_pointer',
);
// Check if screen related pointer is registered
if ( empty( $registered_pointers[ $hook_suffix ] ) )
return;
$pointers = (array) $registered_pointers[ $hook_suffix ];
/*
* Specify required capabilities for feature pointers
*
* Format:
* array(
* pointer callback => Array of required capabilities
* )
*
* Example:
* array(
* 'wp390_widgets' => array( 'edit_theme_options' )
* )
*/
$caps_required = array(
'wp500_isrc_pointer' => array(
'install_plugins'
),
);
// Get dismissed pointers
$dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
$got_pointers = false;
foreach ( array_diff( $pointers, $dismissed ) as $pointer ) {
if ( isset( $caps_required[ $pointer ] ) ) {
foreach ( $caps_required[ $pointer ] as $cap ) {
if ( ! current_user_can( $cap ) )
continue 2;
}
}
// Bind pointer print function
add_action( 'admin_print_footer_scripts', array( 'isrc_Internal_Pointers', 'pointer_'.$pointer ) );
$got_pointers = true;
}
if ( ! $got_pointers )
return;
// Add pointers script and style to queue
wp_enqueue_style( 'wp-pointer' );
wp_enqueue_script( 'wp-pointer' );
}
/**
* Print the pointer JavaScript data.
*
* #since 3.3.0
*
* #static
*
* #param string $pointer_id The pointer ID.
* #param string $selector The HTML elements, on which the pointer should be attached.
* #param array $args Arguments to be passed to the pointer JS (see wp-pointer.js).
*/
private static function print_js( $pointer_id, $selector, $args ) {
if ( empty( $pointer_id ) || empty( $selector ) || empty( $args ) || empty( $args['content'] ) )
return;
?>
<script type="text/javascript">
(function($){
var options = <?php echo wp_json_encode( $args ); ?>, setup;
if ( ! options )
return;
options = $.extend( options, {
close: function() {
$.post( ajaxurl, {
pointer: '<?php echo $pointer_id; ?>',
action: 'dismiss-wp-pointer'
});
}
});
setup = function() {
$('<?php echo $selector; ?>').first().pointer( options ).pointer('open');
};
if ( options.position && options.position.defer_loading )
$(window).bind( 'load.wp-pointers', setup );
else
$(document).ready( setup );
})( jQuery );
</script>
<?php
}
/**
* Display a pointer for wp500_isrc_pointer
*
* #since 4.9.6
*/
public static function pointer_wp500_isrc_pointer() {
$content = '<h3>' . __( 'Personal Data and Privacy' ) . '</h3>';
$content .= '<h4>' . __( 'Personal Data Export and Erasure' ) . '</h4>';
$content .= '<p>' . __( 'New <strong>Tools</strong> have been added to help you with personal data export and erasure requests.' ) . '</p>';
$content .= '<h4>' . __( 'Privacy Policy' ) . '</h4>';
$content .= '<p>' . __( 'Create or select your site’s privacy policy page under <strong>Settings > Privacy</strong> to keep your users informed and aware.' ) . '</p>';
if ( is_rtl() ) {
$position = array(
'edge' => 'right',
'align' => 'bottom',
);
} else {
$position = array(
'edge' => 'left',
'align' => 'bottom',
);
}
$js_args = array(
'content' => $content,
'position' => $position,
'pointerClass' => 'wp-pointer arrow-bottom',
'pointerWidth' => 420,
);
self::print_js( 'wp500_isrc_pointer', '#menu-settings', $js_args );
}
/**
* Prevents new users from seeing existing 'new feature' pointers.
*
* #since 3.3.0
*
* #static
*
* #param int $user_id User ID.
*/
public static function dismiss_pointers_for_new_users( $user_id ) {
add_user_meta( $user_id, 'dismissed_wp_pointers', 'wp500_isrc_pointer' );
}
}
What you are looking for is WordPress Activation / Deactivation Hooks. For example:
register_activation_hook( __FILE__, 'pluginprefix_function_to_run' );
And on pluginprefix_function_to_run, display a nice message to let users know that you've added a menu using admin_notices:
function my_admin_notice() {
?>
<div class="updated">
<p><?php _e( 'Your message goes here!', 'my-text-domain' ); ?></p>
</div>
<?php
}
function pluginprefix_function_to_run() {
add_action( 'admin_notices', 'my_admin_notice' );
}

Create template for custom post types in Wordpress

I know how to create a custom template for a specific page. However I would like to create a template for a specific custom post type. Is that possible and if true how can I do that?
If I create a new template it will show in admin only when I'm adding a page, but when I'm adding a new post type I don't have the option to select a certain template.
Problem resolved:
/*
Show the list of available custom templates templates in the Custom Post Type admin section
*/
/**
* Post_type
*/
define( 'MY_THEME_POST_TYPE', 'cases' );
/**
* Load the page template for any post object
* having the appropriate meta key set.
*/
add_action( 'template_redirect', 'mytheme_template_redirect' );
function mytheme_template_redirect() {
global $wp_query;
$id = (int) $wp_query->get_queried_object_id();
$template = get_post_meta( $id, '_wp_page_template', true );
if ( $template && 'default' !== $template ) {
$file = STYLESHEETPATH . '/' . $template;
if( is_file( $file ) ) {
require_once $file;
exit;
}
}
}
/**
* Process the Meta Box
* #todo Permissions check.
* #todo Filter input.
* #todo Nonces.
*/
add_action( 'save_post', 'mytheme_process_resource_template' );
function mytheme_process_resource_template() {
global $post;
/* Sanitize $_POST array. */
$clean_id = ( isset( $_POST['ID'] ) ) ? intval( $_POST['ID'] ) : 0;
if ( !empty( $_POST['page_template'] ) && MY_THEME_POST_TYPE == $post->post_type ) {
$page_templates = get_page_templates();
if ( 'default' != $page_template && !in_array( $_POST['page_template'], $page_templates ) ) {
if ( $wp_error )
return new WP_Error('invalid_page_template', __('The page template is invalid.'));
else
return 0;
}
update_post_meta( $clean_id, '_wp_page_template', $_POST['page_template'] );
}
}
/**
* Registers the Meta Box
* #uses mytheme_page_attributes_meta_box()
*/
add_action( 'admin_init', 'mytheme_register_meta_boxes', 10 );
function mytheme_register_meta_boxes() {
add_meta_box(
'mytheme_post_type_template',
'Template',
'mytheme_page_attributes_meta_box',
MY_THEME_POST_TYPE,
'side',
'low'
);
}
/**
* Creates the Meta Box
*/
function mytheme_page_attributes_meta_box() {
global $post;
$post_type_object = get_post_type_object($post->post_type);
if ( 0 != count( get_page_templates() ) ) {
$template = get_post_meta( $post->ID, '_wp_page_template', true );
?>
<p><strong><?php _e('Template') ?></strong></p>
<label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php page_template_dropdown( $template ); ?>
</select>
<?php
}
}
Create page that is called:
single-{cpt-slug}.php e.g. single-product.php
It will be used when showing a page of a custom post type. i.e. when someone goes to http://example.com/product/awesome-shoes/

Resources