I am new to both Wordpress development and PHP and I am currently following this tutorial that I really enjoy: https://www.youtube.com/watch?v=hbJiwm5YL5Q
But it did not take long until my code (even though eyactly the same as in the tutorial to my findings) stopped behaving as expected. The do_settings_sections doesn´t render the field.
What am I doing wrong? Unlike in this question I initialized the function, didn´t I?
My code:
<?php
/*
Plugin Name: Word Count Statistics
Description: A plugin to display the word and character count, as well as the aproximate reading time for an article. Where and what to display can be customized on the settings page.
Version: 1.0
...
*/
//using a class allows the usage of function names, that don´t need to be unique
class WordCountAndTimePlugin
{
function __construct()
{
//array allows referencing the function names in the class
add_action('admin_menu', array($this, 'adminPage'));
add_action('admin-init', array($this, 'settings'));
}
function adminPage()
{
add_options_page(
'Word Count Settings', //Title of page
'Word Count', //Display name in menu
'manage_options', //Access allowed if user may...
'word-count-settings-page', //Slug name
array($this, 'displayHTML') //Function to call
);
}
function displayHTML()
{
?>
<div class="wrap">
<h1>Word Count Settings</h1>
<form action="options.php" method="POST">
<?php
do_settings_sections('word-count-settings-page');
submit_button();
?>
</form>
</div>
<?php
}
function settings()
{
//Creates a form section
add_settings_section(
'wcp_first_section', //Section name
'some text here', //Subtitle
null, //Content / Desription
'word-count-settings-page' //Slug name
);
//Creates a setting field
add_settings_field(
'wcp_location', //Name of setting
'Display Location', //HTML label text
array($this, 'displayLocationHTML'), //Function to display custom HTML
'word-count-settings-page', //Slug name
'wcp_first_section' //Section name
);
//Registers the setting field to the WordPress settings
register_setting(
'wordcountplugin', //Group it belongs to
'wcp_location', //Name of setting
array(
'sanitize_callback' => 'sanitize_text_field', //Validates value
'default' => '0' //Default value
) //Array of options
);
}
function displayLocationHTML()
{
?>
Hello
<?php
}
}
$wordCountAndTimePlugin = new WordCountAndTimePlugin();
?>
Thanks in advance :)
You have a small mistake in the hook`s name. You need to replace:
add_action('admin-init', array($this, 'settings'));
on:
add_action('admin_init', array($this, 'settings'));
Related
I am working on a plugin and I am creating a meta box on my page using the below code but it's not displaying anything.
Any idea where is the issue with my code?
class bt_dashboard {
function __construct() {
add_meta_box( 'metaboxes-sidebox-1', 'Sidebox 1 Title', array( $this, 'welcome_bt' ), 'welcomebt', 'advanced', 'default' );
}
function setup() {
// setup class, maybe add hooks
}
function welcome_bt() {
?>
<h2>Wellcome to metabox</h2>
<?php
}
}
Meta box not displaying on the plugin page
I am building a plugin for Wordpress, the page of the plugin is created according the following code:
public function menu_html()
{
?>
<form method="post" action="options.php">
<?php settings_fields('zero_polls') ?>
<?php do_settings_sections('zero_polls') ?>
<?php submit_button(); ?>
</form><?php
}
So above I instantiate the field name "zero_polls" which is created here:
public function register_settings()
{
register_setting('zero_polls', 'zero_polls_question');
register_setting('zero_polls', 'zero_polls_add');
add_settings_section('polls_section', 'Sondage', array($this, 'section_html'), 'zero_polls');
add_settings_field('zero_polls_question', "Question:", array($this, 'question_html'), 'zero_polls', 'polls_section');
add_settings_field('zero_polls_add', 'Add a new answer:', array($this, 'add_html'), 'zero_polls', 'polls_section');
}
And the menu_html function is called when Wordpress update the menu with:
public function add_admin_menu()
{
$hook = add_submenu_page('options-general.php', 'Sondages', 'Sondages', 'manage_options', 'zero_polls', array($this, 'menu_html'));
add_action('load-'.$hook, array($this, 'process_action'));
}
As you can see above, I added an action which redirect process_action() function here:
function process_action(){
global $wpdb;
$wpdb->query("INSERT INTO wp_poll_options(id, label) VALUES('','YOUPI')");
}
The problem is that my table wp_poll_options does not get update after I submit the form (YOUPI does not appears).
I think I missing something in the understanding of the Wordpress workflow, please help, thank you.
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
I'm having issues getting my field to save using WordPresses setting API
I have 1 option page, with 1 section and 1 field. So it's pretty straight forward. Everything appearing/rendering fine, but I can't save the fields.
class LocalSEOAdmin {
var $slug = 'local_seo_settings';
var $name = "Local SEO";
public function __construct(){
// When WP calls admin_menu, call $this->admin_settings
if ( is_admin() ) {
add_action('admin_menu', array( &$this, 'add_options_page' ) ); // on admin_menu call $this->init_admin_menu
add_action( 'admin_init', array( &$this, 'add_setting_fields' ) );
}
}
public function add_options_page(){
add_options_page(
$this->name, // Page title
$this->name, // Menu Title
'manage_options', // Role
$this->slug, // Menu slug
array( &$this, 'local_seo_admin_menu_callback' ) // Callback to render the option page
);
}
public function local_seo_admin_menu_callback(){
?>
<div class='wrap'>
<h1><?php echo $this->name ?> Options</h1>
<form method='post' action='options.php'>
<?php
settings_fields( 'address_settings_section' );
do_settings_sections( $this->slug );
submit_button();
?>
</form>
</div>
<?php
}
public function add_setting_fields(){
add_settings_section(
'address_settings_section', // ID used to identify this section and with which to register options
'Address Options', // Title to be displayed on the administration page
array( &$this, '_render_address_options'), // Callback used to render the description of the section
$this->slug // Page on which to add this section of options
);
add_settings_field(
'address_line_1', // ID used to identify the field throughout the theme
'Address Line 1', // The label to the left of the option interface element
array( &$this, '_render_address_line_1'), // The name of the function responsible for rendering the option interface
$this->slug, // The page on which this option will be displayed
'address_settings_section', // The name of the section to which this field belongs
array( // The array of arguments to pass to the callback. In this case, just a description.
__( 'Activate this setting to display the header.', 'sandbox' ),
)
);
register_setting(
'address_settings_section',
'address_settings_section'
);
}
public function _render_address_options(){
echo '<p>Add you address</p>';
}
public function _render_address_line_1(){
$option = get_option( 'address_line_1' );
$html = '<input type="text" id="address_line_1" name="address_line_1" value="' . $option . '" />';
echo $html;
}
}
new LocalSEOAdmin;
I'm not sure what's going on, but I feel I've mixed up the required fields someone. The fields are showing in WordPress and when I hit update the "setting saved." messages comes up, but nothing gets saved.
there is just a little problem when you register your setting.
base on register_setting, the second parameter, is $option_name so you must put send name of you inputs
register_setting(
'address_settings_section',
'address_line_1'
);
and now it's worked.
you can find more about creating options pages in wordpress site.
In register_setting()
First paramerter is your option group name &
Second parameter is option name which is given in add_settings_field() , in your case is should be address_line_1 not address_settings_section
Finally
register_setting(
'address_settings_section',
'address_line_1'
)
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);
})