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' );
}
}
Related
I'm using a plugin which adds a submenu item to an admin menu like so:
add_submenu_page( 'propertyhive', 'Property Hive Settings', 'Settings', 'manage_options', 'ph-settings', 'callback_fn' );
Due to it stating manage_options it only appears for administrators. I need to show it for editors. Here's what I've tried in my theme's functions.php file:
add_action( 'admin_menu', 'custom_settings_menu', 99 );
function custom_settings_menu()
{
// Remove the submenu item first
remove_submenu_page( 'propertyhive', 'ph-settings' );
// Add it again but with different role (manage_propertyhive)
// This role does exist as other submenu items ue it
add_submenu_page( 'propertyhive', 'Property Hive Settings', 'Settings', 'manage_propertyhive', 'ph-settings', 'my_theme_callback_fn' );
}
Although this does show the submenu item correctly, I get the following error:
Sorry, you are not allowed to access this page.
Can anyone see anything obvious or have any inclinations as to what might cause this?
Note: The manage_propertyhive capability definitely does exist
I believe this is happening because 'manage_propertyhive' is not a defined capability, therefore nobody will have access to that menu. You can either use one of the predefined wordpress capabilities which you can find here or you can define your own custom capability such as 'manage_propertyhive', by following the instructions here.
Hope this helps!
1) Are you sure the add_submenu_page() function from the Plugin is copied correctly? add_submenu_page() accepts only 6 parameters - in your question it has 7 paramters with propertyhive being the capability and manage_options being the menu_slug (which is perplexing)
https://developer.wordpress.org/reference/functions/add_submenu_page/
2) I guess that administrators as well as editors got the capability manage_propertyhive ? If not make sure.
3) In your sample code the callback function for the new propertyhive submenu page is my_theme_callback_fn - did you insert the correct callback function here?
4) The fact that you add the submenu page to editors does not necessarily mean they can access that page - did you check the Plugin for further capability checks? Maybe in the code of the callback function or in some other function of the Plugin capabilities are checked again and editors are missing some capability.
This must do the trick
function add_theme_caps() {
$role = get_role( 'editor' );
$caps = (array)$role->capabilities;
if(!array_key_exists('manage_propertyhive', $caps)) {
$role->add_cap( 'manage_propertyhive' );
}
}
add_action( 'admin_init', 'add_theme_caps');
Assuming you've set up the capability right, perhaps the parent page is not allowing access to submenu page, make sure user is able to access parent page...
This is the function that checks if user can access the page... If it returns false the error is displayed...
https://github.com/WordPress/WordPress/blob/4.6.1/wp-admin/includes/plugin.php#L1697-L1763
Besides checking several other things, it also checks if parent page is accessible by the user.
If that doesn't work, I'd suggest you to go to this file on you local install, and var_dump all vars that are checked before returning false, this is how we devs debug the errors... ;)
Make sure to restore the file to original file ( I just update the WordPress again, that restores all core files to their original state )...
Hope that helps.
Added the line of code in my function.php file
add_theme_support('post-thumbnails');
After going saving, and looking at my screen options, Featured images still wasn't showing. I even went as far back to check if my Custom Fields was correct and I had it unmarked to show
First: (I assume it's a typo in your description): Change function.phpto functions.php
Second: Try to call add_theme_support('post-thumbnails') within a function attached to the after_setup_theme hook as shown in the documentation.
// Register Theme Features
function custom_theme_features() {
// Add theme support for Featured Images
add_theme_support( 'post-thumbnails' );
}
// Hook into the 'after_setup_theme' action
add_action( 'after_setup_theme', 'custom_theme_features' );
I hope these steps will fix your issue, otherwise it would be necessary to provide more of your code
hi #EliCollins Use this code. It should work
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('post-thumbnails' );
}
I actually found out the answer just last night. In the CPT UI on the left hand side there is a settings area "Click headings to reveal available options.", click on the settings caret and at the bottom under supports select Featured Images. And then of course going into your function.php and adding
add_theme_support('post-thumbnails' );
After updating (or maybe not) but some time before, when I'm going to category list page mysite.com/?cat=10 I'm getting not category.php template - it opens single.php Why?
I was trying to make category-10.php (just for test) but still coming to single.php.
Where I should look about my problem?
The problem was with my ajax plugin his code is :
<?php
/**
* Plugin Name: Ajax content loader
* Description: Load content througt ajax.
* Version: 0.1
*
*/
/**
* Initialization. Add our script if needed on this page.
*/
function ajax_content_init() {
global $wp_query;
// Add code to index pages.
if( !is_singular() ) {
// Queue JS
wp_enqueue_script(
'load-posts',
plugin_dir_url( __FILE__ ) . 'js/load-posts.js',
array('jquery'),
'1.0',
true
);
$wp_query->is_single = true;
}
}
add_action('template_redirect', 'ajax_content_init');
?>
Any ideas what is wrong here?
It is for mobile version, how to activate him only for mobile? not for desktop?
Download the WordPress Debug Bar Plugin. It will add a button to your wp-admin bar that says "Debug". When pressed, this button will show you the debug panel. Find the tab in this debug panel that says "WP Query", and it should show you query vars and the template file that are being used.
This should help you figure out what query vars are being registered, and which template file is really loaded.
You should read wordpress codex for template_redirect before trying to experiment things.
This action hook executes just before WordPress determines which
template page to load.
What are you actually trying is:
if( !is_singular() ) {
If page is not a singular, means is not a template page.php or single.php it should execute the code.
So when you were at category.php it executed the code and sets the global $wp_query layer objects property single to true. So whatever template you were viewing at that time it will set that page as single.
Hope it helps.
i created a wp plugin, now, whenever i change it through
a) editor,
b) manually in Sublime Text2 and then delete from wp, and re-upload the changed version
and go to page with shortcode that's supposed to trigger plugin, i see no changes. i now have just few words and no html tags whatsoever in php page its supposed to include, it still displays the old html table that was there
here's the code i use to activate the plugin when i 'install' it in WP
register_activation_hook( __FILE__, 'act');
function act(){
add_option('Activated_Plugin','Plugin-Slug');
/* activation code here */
}
function getxml( $atts ){
include 'getxml.php';
}
add_shortcode( 'boixml', 'getxml' );
Did you install W3 Total Cache or WP Super Cache plugins?
Try that link may it give you the key to solve that problem
http://codex.wordpress.org/WordPress_Optimization/Caching
Learning php, figure as well as following tutorials doing some practical useful stuff is import. So a wordpress plugin....
Trying to remove the favorite actions box that you get in the wordpress admin header.
<?php
/*
Plugin Name: Hide Favorite Actions
Plugin URI: http://www.mysite.com
Description: Allows you to remove the Screen Options and Help tabs from view
Author: Tai Havard
Version: 1.0
Author URI:
*/
add_action('admin_menu','removeHelpAndScreenOptions');
function removeHelpAndScreenOptions()
{
remove_action('favorite_actions');
}
?>
Plugins activated, function runs, I'm just not sure how to get hold of the favorite_actions correctly and wether remove_action is the correct function for use with the favorite_actions hook.
Thanks
Here's how remove action works:
remove_action( 'hook_name', 'function_name' );
That says you want to remove the function function_name from the hook hook_name. I don't know what the hook and function are fore removing help and screen options, though. If I remember correctly, those tabs are hardcoded into the actual admin pages.
I used that code and got an error in a template.php (presumably expecting an array) The box disappears if you return with an empty element, something like this:
add_filter('favorite_actions', 'no_fav');
function no_fav($actions) {
$actions = array(
'' => array(__(''), '')
);
return $actions;
}
I just deleted the strings, somebody could probably write a more elegant empty array.
In your plugin just add
function rb_ax() {
return;
}
add_filter( 'favorite_actions', 'rb_ax' );
And you're done.
this works for me, wp 3.0.5
/**
* Remove "Favorite actions" from Admin
*/
add_filter('favorite_actions', 'no_fav');
function no_fav($actions) {
return array();
}
I put it in functions.php, but it would probably work fine as a plugin.
returning nothing (void?) works, but writes Warning: Invalid argument supplied for foreach()...