Wordpress plugin functions: check if function exists - wordpress

I'm developing a Wordpress site that relies on a plugin to be activated for the site to function properly.
The plugin has a few useful functions that I'm using in the site's template files. When the plugin is active, everything works perfectly. If the plugin is deactivated, the content doesn't load.
Wrapping these functions in if(function_exists(...) obviously fixes that, but I'm wondering if there's a cleaner way of doing that in Wordpress. Is there a function that can be placed in the theme's functions.php file that can check if these functions are available every time I call them, and if not provide a safe fallback without me having to wrap them in the function_exists()?
Thanks.

If you're only using it sparingly (1-2 times), use if( function_exists() ). If you're calling the function several times through in different template files, I'd suggest using something like
In your functions.php
function mytheme_related_posts( $someparams = nil ) {
if( function_exists( 'related_posts' ) ) {
related_posts( $someparams );
} else {
echo 'Please enable related posts plugin';
}
}
Then use mytheme_related_posts() in your template.

I think this is the most clear way. It prevents all problems. I think you can write a function instead which can check if these functions are available every time you call them, but I'm almost sure it can cause you more trouble and it burns more memory then simply using if(function_exist()). Don't forget the else branch and it will work fine.

If you want to check if a plugin is active then you should be using the is_plugin_active() function - you can find the docs at: http://codex.wordpress.org/Function_Reference/is_plugin_active
You can then also use if(function_exists()) as well just to doubly make sure :)

Related

How to set a variable on functions.php only if the viewed page is the homepage

i am trying to run some PHP code on functions.php to clean some CSSs and add another ones. For now, for testing purpuse i am passing a parameter on the URL and if the parameter is present the code run else, the web load without the PHP code. I need to do a lot of tests on every URL of the site before leave the code running on all the web.
For now, the code is working only for homepage and I want to leave the code running, when i optimize another page i will to add it, etc etc, but I can't use the is_front_page() or is_page() function on functions.php, so what can I do?
I tried adding add_action but this is running after my PHP code, is there a way to use the is_front_page() function on functions.php or i need to change the logic of my code?
Note: I am a developer but I don't know enough about the Wordpress core and how to code in WP.
Note2: I read a lot of questions here and none of them help me.
I don't know your code, but maybe it's ok for you to write your code in a function in functions.php and use that function in page.php template?
How does your add_action code looks?
This should work too:
add_action('get_header', function() {
if ( is_front_page() ) {
// do it
}
});

confilict between two plugins

I have two plugins (local code - not downloads) that import data from XML files.
The first plugin works fine. I then duplicated the plugin to work on a completely separate XML file. I renamed the directory/plugin-name/plugin-code.
When I activate the second plugin two weird things happen. The original plugin tries to activate the import code for the second plugin - how it does that I have no idea. Secondly, the second plugin gives a 500 error after it completes.
I deactivated the first plugin and tried the second plugin and it works fine. After re-activating the first plugin it fails.
Is there something in wordpress that causes this sort of conflict?
I know this question is not specifically code based but I guess the answer is.
I am just not smart enough to know how wordpress would create this conflict.
Unfortunately, You can't use same plugin copy. Because of how programming languages work, loading the same plugin twice will result in both versions trying to define the same functions and classes. PHP won't stand for that, and will die with a fatal error.
If you want to use the plugin before that you have rename classes and function of that plugin file. A main reason for check existence of function.
if ( ! function_exists( 'my_awesome_function' ) ) {
/**
* My Awesome function is awesome
*
* #param array $args
* #return array
*/
function my_awesome_function( $args ) {
//function stuff
return array();
}
}
In above code this will check if function not exist then declare it.
A plugin is basically a series of functions. By duplicating your plugin you are duplicating your functions which is causing the conflict. If you want to duplicate your plugin you won’t just need to duplicate and rename the folder but you’ll also need to rename your functions in the second plugin, otherwise you’ll be redeclaring already declared functions.

WordPress using different CSS - is this possible?

Bit is a basic question here but can someone confirm that this statement be confirmed: WordPress Pages (certain templates created within) can pull different CSS and JS?
Or - does WordPress only permit universal CSS + JS to be pulled across the entire site?
Thanks for clearing this up.
Depends on what plugin and themes you use. The WordPress/PHP functions wp_enqueue_style() and wp_enqueue_script() can be used literally by everyone (core, themes, plugins, you) to request WordPress to load styles or JavaSctript. You can combine this with WordPress functions to check whether the current page is something you want to filter for (post type, post, front-page, category archive, template, etc.). Here is an example to load a custom style if on front page :
if (is_front_page()) {
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
}
You will have to hook this piece of code to the wp_enqueue_script action so that WordPress executes it at the appropriate time. Here is an example using an anonymous function:
add_action('wp_enqueue_scripts', function() {
if (is_front_page())
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
});
You can also register your code as a "normal" function and pass the functions name to add_action() instead.
Edit: Enabling and disabling plugins is a bit more difficult, since you can never know how they implement their features without examining the source code. Here are my thoughts on this:
The plugin likely uses the above method (wp_enqueue_styles, wp_enqueue_scripts) to register it's styles and scripts. The plugin, since it assumes to be needed on all pages and posts, does this on every page without the conditional checking described earlier.
You could do one of the following to stop the plugin from doing this:
Identify the place where the plugin loads the styles and scripts and add the if-statement to only do so if the post-ID matches your desired post-ID. This method is bad since your changes are lost every time the plugin is updated.
Write a "counter plugin" (you could just add it to your theme or find a plugin that allowes you to add PHP to your page) that "dequeues" the style and script added by the plugin with inversed conditional tag
The counter-plugin approach would look as follows:
function custom_unregister_plugin() {
if (not the desired blog post) {
wp_dequeue_style('my-plugin-stylesheet-handle');
wp_dequeue_script('my-plugin-script-handle');
}
}
Make sure this function is executed after the enqueuing-code of your plugin by giving it a low priority in the same hook (999 is just an example, test it yourself):
add_action('wp_enqueue_scripts', 'custom_unregister_plugin', 999);
With wp_enqueue_style() you can add stylesheet (https://developer.wordpress.org/reference/functions/wp_enqueue_style/)
You can use it after detecting which template is used
function enqueue_custom_stylesheet() {
if(get_page_template() == 'contact.php')
wp_enqueue_style( 'contact-style', get_template_directory_uri().'/contact.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_custom_stylesheet' );
You can use wp_enqueue_style for your CSS, wp_enqueue_script for your JS, wp_localize_script to pass variables from PHP to JS.
You can call these with hooks like:
funtion enqueue_my_stuff()
{
// your enqueue function calls
}
add_action('wp_enqueue_scripts','enqueue_my_stuff'); //front end
add_action('admin_enqueue_scripts','enqueue_my_stuff'); //admin panel
add_action('login_enqueue_scripts','enqueue_my_stuff'); //login screen

Calling wp_enqueue_media() in a custom theme widget on WordPress 3.5.x cause js error

I am writing a custom widget for my own WordPress theme.
From WordPress 3.5 there is a new Media Uploader instead of the old ThickBox.
My widget used to work fine on WordPress versions older than 3.5, but now the new media uploader prevent the old working behavior.
I added a check in the costructor for the presence of wp_enqueue_media function:
if( function_exists( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
but when this part of cose is executed javascript throw an error in the console stopping Js engine:
Uncaught TypeError: Cannot read property 'id' of undefined load-scripts.php:69
I removed all the widget code and reduced it to bare bones... the error is caused by wp_enqueue_media() calls, but I cannot get my head around why and how to fix it.
I also read Wordpress 3.5 custom media upload for your theme options, but there is no mention to this issue
Can anyone point me in the right direction? Is there any documentation available for the the WordPress 3.5 Media Uploader?
It's too late for you now, but might be helpful for other people. I managed to make it work using
add_action( 'admin_enqueue_scripts', 'wp_enqueue_media' );
Hope it helps!
The problem you are experiencing is because you probably put your custom jquery in the header and you didn't registered wordpress jquery. If multiple jquery are defined you will get that error.
My sugestion is you should either remove your jquery script or remove the one from wordpress
function remove_jquery() {
wp_deregister_script('jquery');
//wp_register_script('jquery', ("//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"), false);
}
if(!is_admin()){add_action('init', 'remove_jquery');}
I suggest you use the jquery wordpress provides you, if not, the proper way to enqueue it is to deregister the default one an register your jquery. Just remove the comments from the remove_jquery function.
Also, the above code should go in functions.php
Cheers.
From codex [1], the function wp_enqueue_media( $args ) should be called from 'admin_equeue_scripts' action hook. or later.
Example:
function enqueue_media() {
if( function_exists( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
}
add_action('admin_enqueue_scripts', 'enqueue_media');
Hope it helped.
[1]. https://codex.wordpress.org/Function_Reference/wp_enqueue_media
To debug, you need to get the non-minified versions of the js sent to the browser. See the docs:
SCRIPT_DEBUG
SCRIPT_DEBUG is a related constant that will force WordPress to use the "dev" versions of core CSS and Javascript files rather than the minified versions that are normally loaded. This is useful when you are testing modifications to any built-in .js or .css files. Default is false.
define('SCRIPT_DEBUG', true);

Extending Contact Form 7 Wordpress plugin by using hooks

I would like to create a plugin that uses the contact form 7 hook, wpcf7_admin_after_mail. I want to use the plugin to interface with a CRM system. What I have thus far is the following:
//plugin header here
function add_to_CRM( $cf7 )
{
if (isset($cf7->posted_data["your-message"]))
{
full_contact($cf7);
} else {
quick_quote($cf7);
}
return $cf7;
}
add_action('wpcf7_admin_after_mail', 'add_to_CRM');
//other functions here
I can't seem to get this working. I can't even get the hook to work and do something like mail me. Anybody have any idea what I'm doing wrong here. Since I have limited Wordpress experience I might me missing the boat completely with what I'm trying to do here. I've Googled for answers to no end.
EDIT: I ended up adding this to the theme's functions.php file and it works perfectly. Thing is, I want to get it working as a plugin. Any help will be appreciated.
Try delaying the add_action() call, something like;
add_action('init', create_function('',
'add_action("wpcf7_admin_after_mail", "add_to_CRM");'));
This actually registers your CF7 hook once WordPress is ready (which is nearer the time functions.php gets loaded in).

Resources