CMB Admin Checkbox if statement - wordpress

I have a checkbox on a Wordpress admin options page that if checked I want to call a file. After hours of searching I am still coming up short. I am trying to use the advice here: https://github.com/WebDevStudios/CMB2/wiki/Tips-&-Tricks#using-cmb2-helper-functions-and-cmb2_init as well as a confitional statement inside. Is there any advice on what I am doing wrong?
function cmb2_init_check_field_value() {
$checkbox_value = cmb2_get_field_value( 'compel_option_metabox', 'compel_checkbox', get_queried_object_id() );
if($checkbox_value == yes) {
require_once( $this->directory_path . '/post-types/staff.php' );
require_once( $this->directory_path . '/post-types/sermons.php' );
}
}
add_action( 'cmb2_admin_init', 'cmb2_init_check_field_value' );

The cmb2_admin_init hook is too early,so you can not use get_the_ID() to get the post

I just encountered this as well. I couldn't figure out how to get the field value function to work. However, I did figure out how to get the value using the get_post_meta function.
eg:
$checkbox_value = get_post_meta( get_the_ID(), 'compel_checkbox', true);
if($checkbox_value == 'on') {

Related

How to enable WordPress plugin activation after checking the variable for truth

I need to make it possible to activate the plugin developed by me only after the condition with the variable is true. Variable data is pulled from the database.
$results = get_option( 'stp_api_settings');
$test_result = $results['stp_api_text_field_0'];
Now, if $test_result returns a non-zero value, the true, plugin is activated; if there is no value, a false, error message is displayed.
I am trying to use the following code:
add_action( 'admin_init', 'check_for_register_key' );
function check_for_register_key()
{
$results = get_option( 'stp_api_settings');
$test_result = $results['stp_api_text_field_0'];
if ($test_result === true ) {
add_action( 'admin_notices', 'error_notice' );
deactivate_plugins( plugin_basename( __FILE__ ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
function register_plugin_notice(){
?><div class="error"><p>The plugin is not registered, go to the options page and enter the activation
key.</p></div><?php
}
But it does not work: The plugin is registered in any case, regardless of the value of the variable. Help solve the issue, please.
Please try this solution.
You need to use register_activation_hook callback function to do some logical representation before plugin activation

How to use "has_term" correctly

Im working with the single product page and I need to have a different image (depending on the category) after the add to cart button.
I tried this code but it doesn't show me the image that I need
add_action ( 'woocommerce_after_add_to_cart_button', 'content_after_button' );
function content_after_button() {
if (has_term( 'Categoria', 'Accesorios' ) ) {
echo 'https://prueba.soygorrion.com.ar/wp-content/uploads/2019/08/iconos2.jpg';
}
I think im using has_term in the wrong way.
What im trying to accomplish is:
I have a parent category that is "Accesorios" and inside that I have other child categories like "Billeteras". For each one of this child categories it has to show a diferent image.
thank you
First of all there is issue with your has_term checking. If you check has_term docs, you can find it takes first parameter as category term and second parameter as taxonomy.
Add secondly you are just echo image url that is not going to display image.
So, do as follows -
add_action ( 'woocommerce_after_add_to_cart_button', 'content_after_button' );
function content_after_button() {
// do check with has_term( $term = '', $taxonomy = '', $post = null )
if( has_term( 'Accesorios', 'product_cat' ) ) {
echo '<img src="https://prueba.soygorrion.com.ar/wp-content/uploads/2019/08/iconos2.jpg" />';
}
}
Since you mentioned that the problem is with has_term function. If you can get the product ID inside add to cart, then you can use this code to get categories and check them:
$categories = wp_get_post_terms($product_id, 'product_cat');
if(in_array("Accesorios", $categories))
{
echo "bla bla";
}
I have tested this code for another purpose, it works fine. I hope this will help. Please inform me when you test it and if you face any errors update your question with your new code.

Using update_meta_data on WooCommerce thank you page

I am trying to limit access to the WooCommerce thank you page so the user can only view it once (At the moment you can paste the URL in another browser and see it still.)
I was thinking of creating/attaching a custom order meta to the order once the thank you page has loaded and then wrapping the whole page template in an if statement that checks if this meta exists. Thus when they paste it into another browser/window the template sees that this metas exists and shows them a different message.
Is this the right way to do it? This is what i have done so far but it doesnt work!
//functions.php
add_action( 'wp_footer', 'hasLoadedPlayPage', 20 );
function hasLoadedPlayPage( $order ){
if( !is_wc_endpoint_url( 'order-received' ) ) return;
$order->update_meta_data('hasLoadedPlayPage', 'Yes');
$order->save();
}
//thankyou.php
$hasAnswered = get_post_meta($order->ID, 'hasLoadedPlayPage', true);
if(! $hasAnswered){
echo "NOT SEEN";
} else {
echo "SEEN";
}
Any guidance anyone could give me would be greatly appreciated!
Thanks
James
Looks good to me except that you need to use add_action('woocommerce_thankyou'... instead of wp_footer. But as woocommerce_thankyou only receives the order id as an argument, you'll need to use $order = wc_get_order($order_id) to get the WC_Order object. Something like:
//functions.php
add_action( 'woocommerce_thankyou', 'hasLoadedPlayPage', 20, 1);
function hasLoadedPlayPage( $order_id ){
$order = wc_get_order($order_id);
$order->update_meta_data('hasLoadedPlayPage', 'Yes');
$order->save();
}
//thankyou.php
$hasAnswered = get_post_meta($order->ID, 'hasLoadedPlayPage', true);
if(! $hasAnswered){
echo "NOT SEEN";
} else {
echo "SEEN";
}

How can I get current version of custom plugin in wordpress

I want to compare the current version of my custom plug-in with update version of plug-in.
I get updated version of plug-in in response from server.
How can I get current version of my plug-in?
You can use get_file_data() function which is available on frontend and backend. For example:
get_file_data('/some/real/path/to/your/plugin', array('Version'), 'plugin');
if you use get_plugin_data() on the frontend it will throw an error Call to undefined function get_plugin_data(). Here is the correct way to get plugin header data.
if ( is_admin() ) {
if( ! function_exists( 'get_plugin_data' ) ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
$plugin_data = get_plugin_data( __FILE__ );
echo "<pre>";
print_r( $plugin_data );
echo "</pre>";
}
You could save you version in the options table for easy retrieval. But you can also use get_plugin_data for more details about a given plugin.
<?php
$data = get_plugin_data( "akismet/akismet.php", false, false );
?>
Please note that get_plugin_data is only available in the WordPress admin, it's not a front-end available function.
For users who land on this question to find out the current version of WordPress itself use this function.
// it will show only numeric value i.e 5.8.2
echo get_bloginfo( 'version' );

Do I have to add_filter() before apply_filters() in Wordpress?

I'm trying to understand Wordpress plugin like:
apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
I'm looking for all codes in Wordpress, I can't find:
add_filter( 'gettext', ....);
Why there is no add_filter for this plugin? Or I missed something? Same thing like:
do_action('wp_loaded');
I can't find:
add_action('wp_loaded', ....);
apply_filters is like, 'if there are any filters with this name, run the attached callbacks with these parameters'. So if there is no add_filter for that name, it means that there is no filter that's going to be run with the apply_filters call at the moment.
The same goes with do_action and add_action.
I am a beginner in PHP - WordPress stack as well, but this is from my understanding.
The plugins call apply_filters without having any add_filter in their codes is to allow the website users to add custom logic to their plugins. We - the users, can add our own function and use add_filter to register our functions.
For example, this piece of code is from the plugin. Normally, it shows all products but it provides us a way to not show a specific product.
// Plugin's
if (apply_filters( 'plugin_show_products', true, $product->get_id() ) ) {
$this->show_products();
}
So, if we - the users, want to customize a bit. We can add our own function as following (maybe in functions.php)
// Our custom changes
function my_own_changes($boolean, $product_id) {
if ( $product_id === 5 ) return false;
return true;
}
add_filter( 'plugin_show_products', 'my_own_changes', 10, 2 );
This translates to: The plugin will behave normally but for my own site, it will not show the product with ID of 5!
I have come across this type of code in a plugin or theme where the apply_filter is used without necessarily having an existing filter or add_filter
In this case, where the apply_filters is used without a filter you will have to call the function again where you want to run it. For example, in the header of a theme.
The following is an example of apply filters used in a function that is again called in the header.php
if ( ! function_exists( 'header_apply_filter_test' ) ) {
function header_apply_filter_test() {
$filter_this_content = "Example of content to filter";
ob_start();
echo $filter_this_content;
$output = ob_get_clean();
echo apply_filters( 'header_apply_filter_test', $output );//used here
}
}
Now in the header.php file, you would have to call this function since it is not hooked anywhere. So, in this case, to display the output in the header you would call the function like this :
<?php header_apply_filter_test(); ?>
You could as well write this code with a hook and it would do the same thing i.e display the output in the header.
add_filter('wp_head', 'header_apply_filter_test');
if ( ! function_exists( 'header_apply_filter_test' ) ) {
function header_apply_filter_test() {
$filter_this_content = "Example of content to filter";
ob_start();
echo $filter_this_content;
$output = ob_get_clean();
echo $output;
}
}
For this second option, you would still have the capability of using apply_filters anywhere else to call the callback function header_apply_filter_test() since the filter now exists.
So the bottom line in my view is a use case since either approach works!

Resources