WordPress not showing my setting in option.php - php

I have to create a plugin in which user will add that plugin it will add a page to take app_id from the user and then I will use the App ID in my JavaScript widget. I have registered settings, diplay_option_page and tried to retrieve the value but still here is some issue that my input field is not showing up on my setting page.
Any idea? This is my first time with WordPress and PHP.
I was following this tutorial and this is my code:
<?php
/*
Plugin Name: Interakt for WordPress
Plugin URI: http://interakt.co
Description: Integrate the Interakt CRM and messaging app into your WordPress website.
Author: Peeyush Singla
Author URI: https://twitter.com/peeyush_singla
Version: 0.1
*/
class PS_Interakt{
public $options;
public function __construct(){
$this->options = get_option('interakt_plugin_options');
$this->register_setting_and_fields();
}
public function add_menu_page(){
add_options_page('Interakt Options', 'Interakt Options', 'administrator', __FILE__, array('PS_Interakt', 'display_options_page'));
}
public function display_options_page()
{
?>
<div class="wrap">
<h2>Interakt Options</h2>
<form method="post",action="option.php">
<?php settings_fields('interakt_plugin_options');?>
<?php do_settings_sections(__FILE__)?>
<p class="submit">
<input name="submit" type="submit" class="button-primary" value="Save" >
</p>
</form>
</div>
<?php
}
public function register_setting_and_fields(){
register_setting('interakt_plugin_options', 'interakt_plugin_options' );
add_settings_section('interakt_main_section', 'App Key Setting', array($this,'interakt_main_section_cb'), __FILE__);
add_settings_field('interakt_app_key', "Interakt App Key", array('PS_Interakt','interakt_app_key_setting'), __FILE__, 'interakt_main_section' );
}
public function interakt_main_section_cb(){
}
public function interakt_app_key_setting(){
echo "<input name = 'interakt_plugin_options[interakt_app_key]' type='text' value='{$this->options['interakt_app_key']}'/>";
}
}
add_action('admin_menu', function(){
PS_Interakt::add_menu_page();
});
add_action('admin_init', function(){
new PS_Interakt();
});

When registering fields for options you have to hook in admin_menu hook within your __construct method.
add_action('admin_menu', array($this, 'register_setting_and_fields'));
I also add the admin_init hook in __construct method as well so it looks self contained.
admin_init need admin_menu to hook in. So in your case it should be
add_action('admin_init', array($this, 'add_menu_page'));
Try this code:
class PS_Interakt{
public $options;
public function __construct(){
$this->options = get_option('interakt_plugin_options');
add_action('admin_init', array($this, 'add_menu_page'));
add_action('admin_menu', array($this, 'register_setting_and_fields'));
}
public function add_menu_page() {
add_options_page('Interakt Options', 'Interakt Options', 'administrator', '__FILE__', array($this, 'display_options_page'));
}
public function display_options_page()
{
?>
<div class="wrap">
<h2>Interakt Options</h2>
<form method="post",action="option.php">
<?php settings_fields('interakt_plugin_options');?>
<?php do_settings_sections(__FILE__)?>
<p class="submit">
<input name="submit" type="submit" class="button-primary" value="Save" >
</p>
</form>
</div>
<?php
}
public function register_setting_and_fields(){
register_setting('interakt_plugin_options', 'interakt_plugin_options' );
add_settings_section('interakt_main_section', 'App Key Setting', array($this,'interakt_main_section_cb'), __FILE__);
add_settings_field('interakt_app_key', "Interakt App Key", array('PS_Interakt','interakt_app_key_setting'), __FILE__, 'interakt_main_section' );
}
public function interakt_main_section_cb(){
}
public function interakt_app_key_setting(){
echo "<input name = 'interakt_plugin_options[interakt_app_key]' type='text' value='{$this->options['interakt_app_key']}'/>";
}
}
if ( is_admin() ) {
new PS_Interakt();
}
is_admin() will ensure that you're in Admin area (Dashboard section)
Better read docs

Your first problem is with OOP concepts, start reading some tutorials asap ;)
Without using static methods, you should only instantiate the class once wrapping everything inside the hook plugins_loaded:
add_action( 'plugins_loaded', function(){
new PS_Interakt();
});
Then inside the constructor, call your working hooks:
public function __construct(){
$this->options = get_option('interakt_plugin_options');
add_action( 'admin_init', array( $this, 'init_admin' ) );
add_action('admin_menu', array( $this, 'add_menu_page' ) );
}
The new init_admin method does the registering:
public function init_admin()
{
$this->register_setting_and_fields();
}
Then, you have errors in your add_options_page, add_settings_section and add_settings_field functions. Check the documentation for each one (Codex/Function_Reference), you're using the parameter $page wrong, it has to be your own page name. So, change all __FILE__ occurrences for my_opts, for example.
Finally, it's much more readable if we use printf and sprintf to build strings from PHP variables, function calls and conditional values:
printf(
"<input name = 'interakt_plugin_options[interakt_app_key]' type='text' value='%s'/>",
$this->options['interakt_app_key']
);

Related

do_settings_sections() is not working with WordPress Plugins PHP

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'));

Add some content to woocommerce product single page via a plugin

I'm somewhat new to developing plugins for Wordpress and I have this idea for a simple woocommerce based plugin. I need this plugin to do something only on single product pages but I can't see to figure out at all how I would add an action when a product gets loaded to do something.
Does anyone have any experience with this?
In my plugin I've attempted to just get this working with some code
public function get_product_variation_data() {
function get_data() {
echo '<script>';
echo "console.log('beep');";
echo '</script>';
}
add_action( 'woocommerce_after_single_product', 'get_data' );
}
But this doesn't seem to run at all on my product page. Maybe there is a different hook I need to attach to?
Try this :
class PluginTest {
public function __construct(){
add_action( 'woocommerce_after_single_product', array($this, 'get_product_variation_data'));
}
public function get_product_variation_data(){
echo '<script>';
echo "console.log('beep');";
echo '</script>';
}
}
$plugin_test = new PluginTest();
You need to add in your plugin constructor __construct() the following:
add_action( 'woocommerce_after_single_product', array( $this, 'get_product_variation_data' ), 5 );
Then outside the constructor:
public function get_product_variation_data() {
?>
<script> console.log('beep'); </script>
<?php
}
This should work…

Wordpress plugin issue

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.

Why does my save_post action not trigger?

I'm writing a plug-in for wordpress in classes and I'm adding an action hook to 'save_post' in the constructor of a class. However it does not seem to fire. Am I using it in the right way?
EDIT 25-05-2014 - I wrote a new (tested) minimal example that definitely reproduces the problem for me.
If I use the save_post in a procedural way (like directly in the index.php) it does work, but obviously that's not helpful when I'm structuring everything in classes.
/*
File index.php
This file handles the installation and bootstrapping of the plugin
*/
define("XX_POST_TYPE", 'testposttype');
if( !class_exists('MyPlugin') ):
class MyPlugin {
var $savecontroller;
public function __construct(){
add_action('init', array($this, 'init'), 1);
//include stuff before activation of theme
$this->include_before_theme();
}
//Include these before loading theme
private function include_before_theme(){
include_once("controllers/savecontroller.php");
}
public function init(){
register_post_type( XX_POST_TYPE,
array(
'labels' => array(
'name' => __('Tests'),
'singular_name' => __('Test'),
'add_new' => __('Add new test'),
'add_new_item' => __('Add new test')
),
'public' => true,
'has_archive' => true,
'hierarchical' => true
)
);
add_action('add_meta_boxes', function(){
$this->savecontroller = new SaveController();
});
}
}
function startup(){
global $myPlugin;
if( !isset($myPlugin) ){
$myPlugin = new MyPlugin();
}
return $myPlugin;
}
//Initialize
startup();
endif;
?>
The save actions happen in a different class and file.
<?php
// file savecontroller.php
class SaveController{
public function __construct(){
add_meta_box('xx_field_box', 'Field', array($this, 'setup_field'), XX_POST_TYPE);
}
public function setup_field( $post ){
?>
<input type="text" name="xx_custom_field" id="xx_custom_field" value="">
<?php
add_action('save_post', array($this, 'save_my_post'), 1, 1);
}
public function save_my_post($post_id){
if(isset($_POST['xx_custom_field'])){
update_post_meta($post_id, 'xx_custom_field', $_POST['xx_custom_field']);
}
}
}
?>
It does create my custom posttype and field so I know the classes are working. But the save_post is not triggered. It does not 'die()' and it does not do the 'update_post_meta()'. The custom field does appear in the POST request so the isset() checks out.
It's probably something dumb, but I can't get it to work.
You're trying to add the hook save_post inside the add_meta_box callback, and that's not the place for it.
To solve it, change the init method to
public function init(){
register_post_type( $args );
$this->savecontroller = new SaveController();
}
And modify the SaveController to
class SaveController{
public function __construct(){
add_action( 'add_meta_boxes', array( $this, 'meta_box' ) );
add_action( 'save_post', array( $this, 'save_my_post'), 10, 2 );
}
public function meta_box(){
add_meta_box( 'xx_field_box', 'Field', array($this, 'setup_field'), XX_POST_TYPE );
}
public function setup_field( $post ){
?>
<input type="text" name="xx_custom_field" id="xx_custom_field" value="">
<?php
}
public function save_my_post( $post_id, $post_object ){
wp_die( '<pre>'. print_r( $post_object, true) . '</pre>' );
}
}
Note that the save_post action takes two parameters and the priority can be default (10). You can find lots of examples for meta boxes and save_post here.

wordpress plugin : Call to undefined function add_menu_page()

I am working on a wordpress plugin and i decided to use OOP instead of functional programming,however;I am receiving this weird error:
Error Message:
Call to undefined function add_menu_page() while am pretty sure everything is working as intended
The Code:
class bdmin{
public function __construct(){
add_action('admin_menu', $this->create_admin_menu());
}
public function create_admin_menu(){
// Create NEW top-level menu
add_menu_page('bdmin Settings', 'bdmin Settings', 'administrator', __FILE__, array( &$this, 'create_admin_page'));
// Register our Settings
add_action( 'admin_init', $this->register_admin_settings() );
}
}
and i initiate the code with $admin = new bdmin();
Best Regards
This should remove the error:
<?php
if(!empty($value['id'])) {
class bdmin{
public function __construct(){
add_action('admin_menu', $this->create_admin_menu());
}
public function create_admin_menu(){
// Create NEW top-level menu
add_menu_page('bdmin Settings', 'bdmin Settings', 'administrator', __FILE__, array( &$this, 'create_admin_page'));
// Register our Settings
add_action( 'admin_init', $this->register_admin_settings() );
}
}
}
?>
To solve this problem all I needed to do is invoke a function named add_action with these parameters.
add_action('admin_menu', array( &$this , 'create_admin_menu'));
Make sure you include the header file at the top so you can make WP function calls
<?php require('{correct_path}/wp-blog-header.php');

Categories