wordpress: display a notice when plugin is activated - wordpress

I read carefully How to display notice in admin panel on Plugin Activation? which is similar, but I could not make it work correctly
class Shoutbox {
function display_notice() {
echo '<div class="updated">
<p>I am a little yellow notice.</p>
</div>';
}
public static function pluginActivated() {
//exit("Plugin has been actived"); // this is displayed when not commented
add_action('admin_notices','display_notice'); // this notice is never displayed.
}
}
//add_action('admin_notices', array('Shoutbox', 'display_notice')); // this is displayed when not commented
new Shoutbox();
I also tried with
public static function display_notice() {
Any idea on how to display the admin notice inside pluginActivated() ?

You need to pass the method to the add_action function as an array, with the first element being the class name, and the second element being the method name. you can follow my code :
class Shoutbox {
public static function display_notice() {
echo '<div class="updated">
<p>I am a little yellow notice.</p>
</div>';
}
public static function pluginActivated() {
add_action('admin_notices', array(__CLASS__, 'display_notice'));
}
}
new Shoutbox();

Related

Add a custom button in WordPress register form

I am trying to add a button in wordpress register form but the position of button is not right as i want. It is looking like the below image
But i want it something like this
Given below is my code
public function init()
{
global $mySetting;
$mySetting = get_option('my_options');
add_action('register_form', [$this, 'my_registration_button']);
add_action('login_form', [$this, 'my_registration_button']);
}
public function my_registration_button()
{
?>
<p>My Button</p>
<?php
}

WordPress add_setting_section does not recognise custom page

I trying to create a custom settings page for my plugin, but I'm unable to make the page display a custom settings page, to make things simple I just want to display the section header.
Here is what I have.
private function add_hooks() {
add_action('admin_menu', array($this,'register_menu'));
add_action("admin_init", array($this,"display_options"));
}
public function register_menu() {
add_menu_page('Feed','API FEED','manage_options','adt-feed',array($this,'include_admin_page'),'dashicons-format-image');
}
public function include_admin_page() {
return include_once( PLUGIN_PATH 'admin/view/config-page.php' );
}
public function display_options()
{
//section name, display name, callback to print description of section, page to which section is attached.
add_settings_section("adt_general_section", "Header Options", array($this, "display_header_options_content"), "adt-feed");
}
public function display_header_options_content() { //THIS NEVER GETS CALLED
echo "PLUGIN HEADER SECTION";
}
and them in my config-page.php i have:
<div class="wrap">
<div id="icon-options-general" class="icon32"></div>
<h1>ADT Feed Options</h1>
<form method="post" action="options.php">
<?php
//add_settings_section callback is displayed here. For every new section we need to call settings_fields.
settings_fields("adt_general_section");
// Add the submit button to serialize the options
submit_button();
?>
</form>
</div>
Using some "die()" calls i manage to find that display_header_options_content never gets called, if i change "adt-feed" for "general" in the add_settings_section, i can see the message: "PLUGIN HEADER SECTION" in the General settings page.
thanks in advance for any help!
You need to call
do_settings_sections( 'adt-feed' );
in config-page.php.
A good place to put this is just before the call to submit_button();

Problems including inline JS in wordpress 4.2.2, wp_add_inline_script not working

i want to put inline js in worpress and according to the wordpress docs the wp_add_line_script is for this, BUT it doesn't exists in the functions.wp-scripts.php file . The other way i tried was using WP_Scripts::add_inline_script but this method doesn't exits as well.
It is funny that wp_add_inline_style works so i can doit with CSS but no with JS..
So i need to include inline JS but cannot use the docs function and i don't want to do
echo "<script>
//code
</script>
So what can i do?, and why this functions are not in wordpress 4.2.2?
Thanks
finally i end up implementing a static method in a helper class that prints the JS before body get closed, i think this can be impruved but for me worked...
With this class i can include an array of js/css files and inline javascript like:
Assets::registerCSS(['jquery-ui.min.css', 'jquery.tagsinput-revisited.css']);
Assets::registerJS(['jquery-ui.min.js', 'jquery.tagsinput-revisited.js'], ['jquery']);
and the inline JS like:
Assets::registerInlineJS($script);
Where $script is the JS code without the tags <script> </script>
This class is a piece of a plugin i'm working and it should work too for other plugins..in my case in the root folder of my plugin i have two folders named css and js thats why i call plugin_dir_url(DIR) . "js/$file" to look for the .js files
Hope this helps somebody
class Assets {
public static function registerJS(Array $files = [], Array $deps = []) {
foreach ($files as $file) {
wp_enqueue_script(basename($file, '.js'), plugin_dir_url(__DIR__) . "js/$file", $deps, false, false);
}
}
public static function registerCSS(Array $files = [], Array $deps = []) {
foreach ($files as $file) {
wp_enqueue_style(basename($file, '.css'), plugin_dir_url(__DIR__) . "css/$file", $deps, false);
}
}
public static function registerInlineJS($js) {
add_action('wp_print_footer_scripts', function() use ($js) {
echo '<script>';
echo $js;
echo '</script>';
});
}
}

Concrete5 - Why is my block controller set() not working?

I have a custom block which has a default view with a form in it. When that form is submitted I set a controller flag and the block is (should be) updated to display more information.
The problem is my view is treating it like I have no data/variables set
Controller.php
public $unlocked = false;
public $employer;
public $shortname = "not loaded";
public function on_page_view() { //already overridden because I'm compiling LESS
...
$this->setViewVariables();
}
function setViewVariables() {
$this->set('shortname', $this->shortname);
$this->set('is_unlocked', $this->unlocked);
...
}
public function action_accesscode_unlock() {
$this->unlocked = true;
$this->shortname = "fred";
//Have also tried calling $this->setViewVariables(); as well,
//before I realised view() and on_page_view() were called after this anyway
}
View.php
<?php if ( !$is_unlocked ) {
echo $shortname; //does correctly display the default value
?>
<form action="<?php echo $this->action('accesscode_unlock')?>" id="accessform" method="post">
...
</form>
<?php } else {
//THIS section is never displayed (always reloads form with default name)
echo $shortname;
} ?>
What am I doing wrong here so that the new variable values are never set in the view?
Edit
After replying to JohnTheFish I just realised, the LESS compilation code I use includes the following lines (used to get block path). Could this be changing the instance used for different parts of the lifecycle?
$bv = new BlockView();
$bv->setController($this);
$bv->setBlockObject($this->getBlockObject());
on_page_view runs before action_accesscode_unlock, so the logic of action_accesscode_unlock does not happen until after the variables are set.
You could try adding a call to setViewVariables to the end of action_accesscode_unlock.
(In answer to your edit, yes, it could)

How to make drag and drop widget and put position into database

How can i make for example Worpdpress Widget.
I want a area dragged and dropped and hold its position into the database.
Does somebody has tips/ideas?
Thanks!
Create a new plugin and open init.php file.
add following code(this is an example code for a widget)
<?php
/*
Plugin Name: Example: My User Widget
Description: This plugin provides a simple widget that shows the name of
the logged in user
*/
class My_User_Widget extends WP_Widget {
function My_User_Widget() {
parent::WP_Widget(false,’My User Widget’);
}
function widget($args) {
$user=wp_get_current_user();
if(!isset($user->user_nicename)) {
$message=’Welcome Guest’;
}
else {
$message=”You are logged in as {$user->user_nicename}”;
}
extract($args);
echo $before_widget;
echo “<p>$message</p>”;
echo $after_widget;
}
}
function register_my_user_widget() {
register_widget(‘My_User_Widget’);
}
add_action(‘widgets_init’,’register_my_user_widget’);
widgets_init hook will call register_my_user_widget method. Then My_User_Widget Class will be called. once you activated the widget you can see it in widgets window and drag and drop it where you want.

Resources