Meta box not displaying on the plugin page - php

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

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

Wordpress does not create multiple widget instances

I thought Wordpress supports creating multiple widget instances in the same location but it looks like it does not in my code. I have created a plugin that shows recent posts. And in it I have created a widget that I want to be able to display multiple times in the same location.
class My_Recent_Posts_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'recent_posts', // Base ID
esc_html__( 'Recent posts', 'text_domain' ), // Name
array( 'description' => esc_html__( 'Display posts via widgets', 'text_domain' ), ) // Args
);
}
And I register the widget like this;
function register_my_recent_posts_widget() {
register_widget( 'My_Recent_Posts_Widget' );
}
add_action( 'widgets_init', 'register_my_recent_posts_widget' );
The problem is if I try to place 2 instances of the same widget in the same location on the Widgets page in the admin the second widget does not show up.
Try this code
class My_Recent_Posts_Widget extends WP_Widget {
public function __construct() {
$widget_ops = array('classname' => 'my_widget_recent_entries', 'description' => esc_html__( "Your most recent Posts.",'text_domain') );
parent::__construct('my-recent-posts', esc_html__('My Recent Posts Widget','text_domain'), $widget_ops);
$this->alt_option_name = 'my_widget_recent_entries';
}

Callback with settings API isn't being called

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

How to add a virtual page (created by a plugin) to the menu builder

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

Targeting a specific `<div>` via Wordpress Plugin

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

Categories