I'm building a WordPress plugin and want to display a message on activating the plugin and on its deactivation.
It should display the message one time on the plugins page in the dashboard.
I have tried it with admin_notices hook but the message continuously displayed.
Can anyone tell me any condition or method to achieve this or any WordPress hook?
Well you can show the message on activation of your plugin only once.
Here is how you can do it:
1) Register a new setting option in your plugin i.e. plugin_status
2) Write this code:
function when_my_plugin_activate() {
if (get_option('plugin_status') != 'active' && !is_plugin_active('your-plugin-folder/your-plugin-file.php')) {
echo 'Your Message Here!';
update_option('plugin_status', 'active');
}
}
add_action('admin_notices', 'when_my_plugin_activate');
3) Here is another code block on deactivation of the plugin to change the status:
function when_my_plugin_deactivate() {
update_option('plugin_status', 'inactive');
}
register_deactivation_hook(__FILE__, 'when_my_plugin_deactivate');
add a setting option and when display message change setting value and use if()
its deactivation
Related
I would love to add a sub menu page on my custom post type called "vouchercodes", but everytime I click on this item in the menu I get "Sorry, you are not allowed to access this page."
I found some articels that "should" work:
https://developer.wordpress.org/reference/functions/add_submenu_page/
https://wordpress.stackexchange.com/questions/63202/how-to-add-a-sub-menu-page-to-a-custom-post-type
But none of those worked for me. I'm sure that I use an full administrator account and I haven't recived any error's in my error logs.
The code I use:
function sens_admin_add_submenus(){
add_submenu_page('edit.php?post_type=vouchercodes', 'Custom Settings', 'Settings', 'manage_options', 'vouchercodes_settings', 'My_Custom_Callback');
}
add_action('admin_init', 'sens_admin_add_submenus');
And my simpel callback function:
function My_Custom_Callback(){
echo "<h1>Welcome to my awsome setting's page</h1>";
}
What am I doing wrong?
Answer Fix from ishio:
Change admin_init to admin_menu
Changing the add_action call from admin_init to admin_menu will correct this.
I want to display an activation message after activating the plugin.
I've seen a few questions on SO about this, but none of 'em work properly:
if (!get_option("startup")) {
echo "<div class='updated'><h3>Welcome to [name]</h3>";
update_option('startup', 'true');
}
That works.. kinda. It puts the HTML at the very top, even before the <!DOCTYPE>. Is there any way to put it at the right place? So in the body tag?
There's a few things you'll need to do here. Firstly, the register_activation_hook() function is used to hook into the activation of your plugin. And the admin_notices action is used to add a notice inside the admin area (you can't just print your notice out anywhere).
However, there's an additional complication in that you can't use the admin_notices action on plugin activation. This is because WordPress doesn't 'live activate' your plugin - it activates it in the background and suppresses the output in order to make sure, before completing the activation, that it doesn't trigger any fatal errors.
Fortunately, this blog post outlines a solution to this problem. The author of the post suggests using transients to save the 'state' of your plugin so that it can be retrieved once it has been activated.
Because that blog has a CC-BY-SA license like this site, I'll copy the code in here so it lives on. I've slightly condensed it to keep the length of this post down, but you can view the whole blog post for the full solution. I've also tested this to ensure it still works - and it does on my install of WordPress 4.5.3.
register_activation_hook( __FILE__, 'fx_admin_notice_example_activation_hook' );
function fx_admin_notice_example_activation_hook() {
set_transient( 'fx-admin-notice-example', true, 5 );
}
add_action( 'admin_notices', 'fx_admin_notice_example_notice' );
function fx_admin_notice_example_notice(){
/* Check transient, if available display notice */
if( get_transient( 'fx-admin-notice-example' ) ){
?>
<div class="updated notice is-dismissible">
<p>Thank you for using this plugin! <strong>You are awesome</strong>.</p>
</div>
<?php
/* Delete transient, only display this notice once. */
delete_transient( 'fx-admin-notice-example' );
}
}
i created a wordpress plugin .when i activated the plugin i got "plugin activated" flash msg. actually i want to customize this message to "connect to my-plugin" and also provide a link to this plugin settings page. i try
add_action( 'admin_notices', 'your_custom_function' );
function your_custom_function() {
echo "connect to my-plugin";
if( $error ) {
// Put everything here
}
}
results connect to my-plugin msg shown on every page in admin panel.i just want to show my custom msg only at the time of activation on plugins page only
also i got an error
The plugin generated 2 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
I guess the one filter hook you are looking for is plugin_action_links_(plugin_file_name) which allows you to add custom links to your plugin entry on the Plugins page.
To display an activation message, see this tutorial.
You could have found these two with some research on your own.
I am trying to hook a function on the entire wordpress backend for a user custom role, just that when the user is accessing the edit posts page post.php?post=xxxx0&action=edit, the function is no more available, printed message disappears.
if ( is_user_logged_in() ) {
echo 'here';
function contributor_posts() {
echo 'here2';
}
add_action( 'admin_init', 'contributor_posts' );
}
echo here - disappears - though it doesn't go on the else
echo 'here2- disappears also
admin_init action is triggered when a logged in user access the admin area, there is no need for is_user_logged_in() check here.
http://codex.wordpress.org/Plugin_API/Action_Reference/admin_init
edit:
Put the code below inside functions.php, admin_init action must be triggered always in every part of the admin area. If that is not the case then i really do not know where the problem is. Visit Wordpress Action Reference to see the list of action hooks available and the order of execution.
function contributor_posts() {
echo 'here';
}
add_action( 'admin_init', 'contributor_posts' );
I'm building a Wordpress plugin and as per the plugin functionality requirement I need to find out if a Wordpress theme has a particular hook or not and in case if it doesn't have then I need to perform some action. Say for eg. show an alert message.
The WordPress function has_action is used for this purpose.
Here's a really basic example showing how you can echo a message if the theme doesn't have the hook you need:
if(has_action('example_hook')) {
do_action('example_hook');
} else {
echo 'an example alert message';
}
There's some more about has_action in the Codex.