Wordpress plugin issue - php

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.

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…

How can i add new item to wordpress sidemenu, with icon on it

How can i add new item to wordpress sidemenu, with icon on it?. So far i have done this , but when clicked , it will redirect to another page. i dont want to redirect to new page . but i want to redirect to logout page
i have searched here on stackoverflow . but no answers so far.
here is what i treid, in functions.php file:
/*
* Logout
*/
add_action('admin_menu', 'logout_menu_item');
function logout_menu_item() {
add_menu_page('Logout', 'Logout', 'manage_options', 'logout', "wp_logout", 'dashicons-external', 999);
}
refrence https://core.trac.wordpress.org/browser/tags/3.2.1/wp-admin/menu.php
<?php
add_action('admin_init', 'logout_menu_item_link');
function logout_menu_item_link() {
global $menu;
$menu[9999] = array(__('Logout'), 'read', wp_logout_url(),'','custom-class','custom-id','dashicons-external');
}
Try Like This:
<?php
add_action('admin_init', 'logout_menu_item_link');
function logout_menu_item_link() {
global $menu;
$menu[9999] = array(__('Logout'), 'manage_options', wp_logout_url());
}
?>
example: Add Icons: https://developer.wordpress.org/resource/dashicons/#album

WordPress not showing my setting in option.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']
);

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