Calling a plugin's function from functions.php - wordpress

I am writing some code in the functions.php of my theme, now what I need to do is execute a function from inside another plugin and im wondering if its possible.
The plugin is wooevents pro and the function I need is as follows:
function order_contains_tickets( $order ) {
// Function code is in here
}
So my code will look something like this
if ( order_contains_tickets( $order ) ) {
// Execute code
}

Technically, the exact code you have would work, but it's also unsafe. In the event that the wooevents pro plugin were ever disabled, the code you added would be referencing a function that no longer exists. Thus, a better option would be to add a check to ensure that the function exists like so...
if( function_exists( 'order_contains_tickets' ) ) {
if( order_contains_tickets( $order ) ){
// Execute code
}
}
It should also be noted that this is dependent on the load order of the two functions. The wooevents pro function must be loaded prior to your function for it to work.

Related

Wordpress / Woocommerce Hook into a Function

I appreciate questions similar to this have been asked before but the answers I've tried aren't doing what I need.
Basically,
I have this file in the woocommerce plugin folder structure:
wp-content\plugins\woocommerce\includes\wc-coupon-functions.php
Inside the file is the following function:
function wc_get_cart_coupon_types() {
return (array) apply_filters( 'woocommerce_cart_coupon_types', array( 'fixed_cart') );
}
I need to override it so it returns an additional item in the array but nothing I've tried has worked. I've tried:
Creating the same file in my custom theme file
hooking the function in my functions file with the following code:
function woocommerce_coupon_get_cart_coupon_types()
{
return (array) apply_filters( 'woocommerce_cart_coupon_types', array( 'fixed_cart', 'custom_discount' ) );
}
add_filter('wc_get_cart_coupon_types', 'woocommerce_coupon_get_cart_coupon_types',10, 1);
Any other suggestions would be greatly appreciated, also..... I've made the change directly in the file and it definitely works. Thanks
Your #2 approach is sort of how to do it, but you're essentially caught in a loop situation the way you did it.
You need to do it this way:
function wpso59974749_woocommerce_coupon_get_cart_coupon_types( $data ) {
$data[ 'your_new_key_here' ] = 'your new value here';
return $data;
}
add_filter('woocommerce_cart_coupon_types','wpso59974749_woocommerce_coupon_get_cart_coupon_types',10, 1);
You shouldn't add the apply_filter back in your function, as it would get stuck in a loop - essentially refiltering itself over and over.
I prefixed your function so if there is another woocommerce_coupon_get_cart_coupon_types function, it won't conflict.

How can I alter the WordPress query without "overriding" it?

I have a search results page that I want to limit results on i.e. posts_per_page. However if I use query_posts('posts_per_page=6') I lose the original query.
How do I alter my query without damaging the original?
You can use the pre_get_posts filter to access/alter the $query object. In functions.php:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('posts_per_page', 6);
}
}
}
add_action('pre_get_posts', 'search_filter');
Never ever use query_posts, it breaks the main query object ($wp_query) and all functionality that relies on the main query object. It also breaks page functionality. Apart from that, it is slow and reruns queries. query_posts should be on top of your most evil list together with functions like create_function(), eval() and extract().
If you need to alter the main query, always use pre_get_posts to do so. Never change the main query with a custom one, it might solve one problem, but it creates many other.
The following will work
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin()
&& $q->is_main_query()
&& $q->is_search()
) {
$q->set( 'posts_per_page', 6 );
}
});

wordpress own plugin has_shortcode does not work

I am using WP 4.3 and I created a plugin which is used only from one single page where I have the shortcode [my_sc].
I tried do realize like this:
add_action('init', 'my_enqu_script');
function my_enqu_script(){
global $post;
$content = $post->post_content;
if(has_shortcode($content, 'my_sc')){
wp_register_script(......);
wp_localize_script(......);
wp_enqueue_script('jQuery');
wp_enqueue_script('my_script');
}
function my_func(){
//do something
}
add_shortcode('my_sc', 'my_func');
}
Unfortunatly, that (has_shortcode) does not work. Any idea?
You shouldn't be wrapping the add_shortcode() bit in the my_enqu_script() function. Instead, add the shortcode, and then hook the other function into an appropriate hook:
function my_func(){
//do something
}
add_shortcode('my_sc', 'my_func');
function my_enqu_script(){
global $post;
$content = $post->post_content;
if ( has_shortcode($content, 'my_sc') ) {
wp_register_script(......);
wp_localize_script(......);
wp_enqueue_script('jQuery');
wp_enqueue_script('my_script');
}
}
add_action('wp_enqueue_scripts', 'my_enqu_script');
You should also consider using the wp_enqueue_scripts action hook, instead of init, since init is too early for global $post; to be in scope.
You just need to create your function and call the function add_shortcode().
e.g.
function my_function(){
echo 'Test';
}
add_shortcode('my_sc', 'my_function');
Also you used : instead of ; which might have been causing the failure.
You should add this to your functions.php file or create a seperate file (best practice) and include() it.

How can I use is_page() inside a plugin?

I want my plugin to register a script only in a certain page.
For example, inside my plugin file I want to write something like this:
if (is_page()) {
$pageid_current = get_the_ID();
$page_slug = get_post($pageid_current)->post_name;
if ($page_slug == 'articles'){
wp_register_script('myscript', '/someurl/main.js');
}
}
But I get the error:
is_page was called incorrectly. Conditional query tags do not work
before the query is run. Before then, they always return false. Please
see Debugging in WordPress for more information. (This message was
added in version 3.1.)
How can I, inside of a plugin, register a script in a certain page?
is_page() only work within template files.
And to use it within plugin files, you need to use it with the combination of template_redirect action hook.
This action hook executes just before WordPress determines which template page to load.
So following snippet would work:
add_action( 'template_redirect', 'plugin_is_page' );
function plugin_is_page() {
if ( is_page( 'articles' ) ) {
wp_register_script( 'my-js-handler', '/someurl/main.js', [], '1.0.0', true );
}
}
You could use is_page() after template redirect so you need to add in the hook like this :
add_action('template_redirect','your_function');
function your_function(){
if ( is_page('test') ) {
// do you thing.
}
}
You must register your script as if you want it to work everywhere.
You can de-register it after the job is done, like this:
function deregister_my_script() {
if (!is_page('page-d-exemple') ) {
wp_deregister_script( 'custom-script-1' );
}
}
add_action('wp_print_scripts', 'deregister_my_script', 100 );

how to add admin_init in my plugin so that contributors can upload images

I Have plugin called MyPlugin written by me. i have a yes or no radio button , If the option is yes means it should allow contributor to upload image and no means it should not allow.
This is the code to allow contributor to upload image
if ( current_user_can('contributor') && !current_user_can('upload_files') )
add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
$contributor = get_role('contributor');
$contributor->add_cap('upload_files');
}
// this is the code to remove if the capabilities if it is added
if ( current_user_can('contributor') && current_user_can('upload_files') )
add_action('admin_init', 'remove_contributor_upload');
function remove_contributor_upload(){
$con = get_role('contributor');
$con->remove_cap('upload_files');
}
i need a help where should i put this code in the plugin, i tried it but i got error as
Error in wp-includes/capabilities.php on line 1059
You are calling functions that can't be called before init(), so they are undefined (specifically, wp_get_current_user() is defined in wp-includes/pluggable.php and that isn't loaded until after all plugins have been loaded). You need to rearrange your code to check user privileges after admin_init is called, e.g.
add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
if ( current_user_can('contributor') && !current_user_can('upload_files') ) {
$contributor = get_role('contributor');
$contributor->add_cap('upload_files');
}
}
// this is the code to remove if the capabilities if it is added
add_action('admin_init', 'remove_contributor_upload');
function remove_contributor_upload(){
if ( current_user_can('contributor') && current_user_can('upload_files') ) {
$con = get_role('contributor');
$con->remove_cap('upload_files');
}
}
NB: just solving your error problem here, have not looked at the logic of what you're doing!

Resources