Creating a wordpress plugin and need this plugin to automatically add posts to wordpress when it installs. What is the better solution to do that?
You should use the register_activation_hook to carry out any custom functions (like adding posts) after the plugin is initialised.
function myplugin_activate() {
// Add your posts here...
}
register_activation_hook( __FILE__, 'myplugin_activate' );
That way, once the plugin is installed, and activated, myplugin_activate() will fire, and carry out any logic you put there.
See: https://codex.wordpress.org/Function_Reference/register_activation_hook for more information.
Related
I'm using "Advanced Scripts plugin" to modify a function of other plugin, the fuction I'm trying to modify is wrappd with if( !function_exists('some_function') ).
the function inside the plugins is like this
if( !function_exists('send-invoice') ){
function send-invoice(){
//The Plugin Invoice
}
}
This is what I did
function send-invoice(){
//My Custom Invoice
}
add_action('init', 'send-invoice');
How can I make sure that my code runs before the plugin codes?
The plugin load before the theme, I tried plugin-loaded hook but nothing changed
You can to use the anonymous function for example:
add_action('init', function() {
//code here
});
More detail is here
Or use another hook muplugins_loaded:
function send-invoice(){
//My Custom Invoice
}
add_action('muplugins_loaded', 'send-invoice');
If nothing else work for you you can create a custom plugin, name it something like "aaamyplugin" and just insert there a single .php file with the function you are trying to override. This is the easiest (not cleanest) way to make sure your code overrides the plugin functions.
The reason for this is because Wordpress plugin loading order is simply alphabetical, that means that everything named before the plugin you are trying to override, get loaded first.
The cleanest way would be to look into the source code of the plugin to understand how it does what it does. Like: when does it load that file that contains the function you are trying to override? That's the important question to answer if you want to go with clean way
I want to customize the behavior of CM tooltip plugin.
From what I see in the code the plugin is a class that has the following filters, which are kind of self descriptive.
class CMTooltipGlossaryFrontend {
/*
* FILTERS
*/
add_filter('get_the_excerpt',array(self::$calledClassName,'cmtt_disable_parsing'), 1);
add_filter('wpseo_opengraph_desc', array(self::$calledClassName, 'cmtt_reenable_parsing'), 1);
/*
* Make sure parser runs before the post or page content is outputted
*/
add_filter('the_content', array(self::$calledClassName, 'cmtt_glossary_parse'), 9999);
add_filter('the_content', array(self::$calledClassName, 'cmtt_glossary_createList'), 9998);
add_filter('the_content', array(self::$calledClassName, 'cmtt_glossary_addBacklink'), 10000);
}
I want to enable/disable the parsing functionality according to my needs ( post type etc).
The plugin code has get_the_excerpt filter, that checks some conditions and disables the parsing. When the wpseo_opengraph_desc gets activated it reanables parsing. The actual parsing happens in cmtt_glossary_parse function.
I wrote a new plugin and tried the following:
I wrote my cmtt_disable_parsing function with higher precedence
add_filter('get_the_excerpt', array($this, 'cmtt_disable_parsing'), 100);
I wrote my cmtt_glossary_parse function which checks the conditions and then calls the CMTooltipClossaryFrontent::cmtt_glossary_parse function
add_filter('the_content', array($this, 'cmtt_glossary_parse'), 10008);
but none of them works. Also, when i instantiate the original plugin inside my plugin, the original plugin is not working properly ( it does not parse the content)
Any help would be appreciated on how to customize properly the plugin functionality.
Should i create a new plugin or is it better to put the code in functions.php?
Is it a bad practice to instantiate a plugin class and call its methods somehwere else, lets say inside another plugin?
Finally i found a working solution for me. So i drop a line here in case someone else finds it useful.
I read this https://iandunn.name/the-right-way-to-customize-a-wordpress-plugin/ guide, which describes the alternatives someone has, if he wants to override a plugin functionality.
In my case the solution was similar as the one described in the "Override their callbacks" section. I downloaded his example that overrides google-authenticator plugin and followed pretty much the same tactic.
Especially for the cm tooltp plugin that i wanted to customize removing the original hooks and re-adding them if my requirements are met worked for me.
remove_filter('the_content', array(CMTooltipGlossaryFrontend::$calledClassName, 'cmtt_glossary_parse'), 9999);
remove_filter('the_content', array(CMTooltipGlossaryFrontend::$calledClassName, 'cmtt_glossary_createList'), 9998);
remove_filter('the_content', array(CMTooltipGlossaryFrontend::$calledClassName, 'cmtt_glossary_addBacklink'), 10000);
and register my callback that enables again the original plugin functions if my requirements are met using the following code
//if (my_condition)
add_filter('the_content', array(CMTooltipGlossaryFrontend::$calledClassName, 'cmtt_glossary_parse'), 9999);
.....
I have a plugin that uses apply_filters like this:
$additional_fields = apply_filters('attachment_meta_add_fields', $additional_fields);
In my theme's functions.php, I do:
function addAttachmentMeta($additionalFields) {
return $addtionalFields;
}
add_filter( 'attachment_meta_add_fields', 'addAttachmentMeta', 1, 1 );
But the function addAttachmentMeta never runs.
How can I alter my apply or add filter statements to make it so that addAttachmentMeta gets called?
Edit:
This is a custom plugin that I wrote based off tutorials on how to add additional attachment meta fields. The whole source is here: http://pastebin.com/7NcjDsK5. As I mentioned in the comments, I know this is running and working because I can add additional fields in this plugin file, but not by using the filters because the filter doesn't get added.
I can see var_dumps before and after the apply_filters statement, but the function I've pointed to with add_filter never gets called.
According to the order WordPress' core loads, function.php gets called after all plugins are loaded and executed.
You need to make sure the apply_filters() in your plugin runs AFTER your add_filter() is called. Otherwise at the point where your filters are 'applied', add_filter() simply hasn't been called yet.
What you could do is use a hook to make that part of your plugin run after functions.php has loaded. You could use the add_action('after_setup_theme', 'function_name') hook.
Wrap the last three lines of your plugin file inside a function and execute it after functions.php runs.
function addAttachmentMeta() {
$additional_fields = array();
$additional_fields = apply_filters('attachment_meta_add_fields', $additional_fields);
$am = new Attachment_Meta( $additional_fields );
}
add_action('after_setup_theme', 'addAttachmentMeta');
I need to check if a user have capabilities to do something in the initialization of a plugin.
I tried with current_user_can() function, and WP_User class, but dont work in these scope.
The question is: I need to check if a user have capabilities in wp-admin, to let he see a specific menu or just to load the content of the plugin.
But how? Both methods above generates a php error.
You should hook your function to run after plugins have loaded
function my_special_function() {
if ( current_user_can( 'do_whatever' ) )
// do your thing
}
add_action( 'plugins_loaded', 'my_special_function' );
Source: http://core.trac.wordpress.org/ticket/23861
I'm quite new to WP. Task is to develop a plugin for oauth authentication on one of not popular openID providers. I did the same for CodeIgniter project, but WP is a CMS and is little bit complex for me to understand. In Codeigniter i check authorisation before each action. In WP i need a hook which uses for it... before each page printing, or maybe.. it would be right to say before each action in terms of frameworks. What is this hook's name?
Last hook before loading the template is template_redirect
You can use it like this:
function my_function(){
// your code goes here
}
add_action( "template_redirect", "my_function" );
You can use init hook. It will be performed before element or HTML code. It's also useful to manage POST and GET variables. The syntax is something like this:
function yourfunction() {
dosomething();
}
add_action('init', 'yourfunction');
A list of all available hooks can be found here: https://codex.wordpress.org/Plugin_API/Action_Reference
Information about Hooks: https://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters
Other hooks must be suggested and will be added in a future release if is a good suggestion.
Or you'd have to edit the core files ;)
You mean a hook when all wordpress function will available but before any output including headers sent?
Well hook your function on init. That will call when visiting site. If you want this hook only for admin area then it is admin_init.