How do I add my class the earliest possible? - wordpress

I am using the following code to add my class “noTypo”:
function prefix_register_block( $blocks ) {
// 'my_block' corresponds to the block slug.
$blocks['wpgb_custom_title'] = [
'name' => __( 'WPGB Custom Title', 'text-domain' ),
'render_callback' => 'prefix_my_block_render',
];
return $blocks;
}
add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 10, 1 );
// The render callback function allows to output content in cards.
function prefix_my_block_render() {
// Get current post, term, or user object.
$post = wpgb_get_post();
// Output the post title.
echo '<span class="noTypo">' . esc_html( $post->post_title ) . '</span>';
}
But it seems that this code above is adding the class later than this code:
function add_notypo_to_title( $title ) {
return "<span class='noTypo'>$title</span>";
}
if (! is_admin() ) {
add_filter( 'the_title', 'add_notypo_to_title', 10, 1 );
}
Both codes add the class successfully, yet the plugin that should use the class for dewidowing – wpTypography -, does not see the class if it’s added with the first code, but it does see the class when it’s added with the second code.
The conclusion is that the class is not available for the plugin at the time it’s processing my content:
add_filter('wp_grid_builder/the_content', [ 'WP_Typography', 'process' ] );
My question is how do I modify the first code so that the class will be added at the earliest possible time, so that at the time wpTypography is processing the content, it would already be available?
The wpTypography plugin does not inspect the final HTML, but it looks for the class at a much earlier stage.
It seems that when I am hooking into the_title in a global function, the class is added earlier.
Is there a way to make my first function global?
Would that make it add the class earlier?
It makes sense that native WP code like the_title runs earlier than plugin code.
I tried to add global $post; into my function, but it will not make the class to be added earlier.
Can I troubleshoot this with Query Monitor to see when are these functions running? And how do I do that?
Does anyone have any other ideas?
According to the wpTypography developer:
what matters is whether the string ultimately passed to
WP_Typography::process as its first parameter contains the element
with the class
How do I verify if the string passed is the first parameter of the process?
Thanks in advance.

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.

WP: overriding recent post widget class

I want to customise the "recent posts" widget.
Since it's not good to edit the core for different reasons, I read about working it in functions.php extending WP_Widget with a new class and overriding the widget($args, $instance) function editing the code I want to customise, next to add it to the widget_init hook but I can't get how it works.
I mean, I think I should extend the WP_Widget_Recent_Posts then tell WP to use my class instead of the original one but....how can I do it?
Thanks
It looks like you can get away just by using the dynamic_sidebar_params() filter along with a search/replace.
add_filter( 'dynamic_sidebar_params', 'replace_widget_class' );
function replace_widget_class( $params ) {
if ( $params[0]['widget_name'] == 'Recent Posts' ) {
$params[0]['before_widget'] = str_replace( 'widget_recent_entries', 'widget_recent_entries_NEW', $params[0]['before_widget'] );
}
return $params;
}

Wordpress shortcode linked to another DB

First thing first, this is my first shortcode attempt.
It is worthed mention that I'm also using woocommerce on my website.
Let's start:
I know that to add shortcodes into wordpress, you need to write something similar to the code below into the functions.php file: (this is just an example)
function myshortcode() {
return "This is a shortcode example!";
}
add_shortcode( 'mycode', 'myshortcode' );
and if i add [mycode] into the wordpress page editor, the preview shows my text correctly.
But what if i need to use a variable (in my case woocommerce order number) in the shortcode?
Let's say i need to compare woocommerce_order_number with my_custom_uid (inserted into another database, outside wordpress).
I usually use a db request like the one below, and usually it works fine (as before, this is only an example):
select 'my_custom_uid' from 'my_custom_database' where 'woocommerce_order_number' = '1234'
The problem is that i don't know the woocommerce_order_number (it changes everytime!), because this shortcode needs to go inside an html email body i need to send out to customers after they placed the order.
How can i get the customer woocommerce order (variable that changes everytime) so that i will be able to use it into my shortcode to link it to my custom_uid?
If the question is not clear enough, please feel free to ask for clarification!
thanks a lot
I don't see a reason to use a shortcode. If you want to add something to an email, you should use one of the hooks in the email. For example:
function kia_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
$some_field = get_post_meta( $order->id, '_some_field', true ),
$another_field = get_post_meta( $order->id, '_another_field', true ),
if( $plain_text ){
echo 'The value for some field is ' . $some_field . ' while the value of another field is ' . $another_field;
} else {
echo '<p>The value for <strong>some field</strong> is ' . $some_field. ' while the value of <strong>another field</strong> is ' . $another_field . '</p>';
}
}
add_action('woocommerce_email_customer_details', 'kia_display_email_order_meta', 30, 3 );
Note that the $order object is the first parameter available to the kia_display_email_order_meta function. So you'd be able to get the ID via $order->id. This should add the data after the customer address details, but there are other hooks available if woocommerce_email_customer_details isn't appropriate.
I finally managed to solve this, and here's what i did if someone is interested:
<?PHP
add_action('fue_before_variable_replacements', 'register_variable_replacements', 11, 4);
add_action('fue_email_variables_list', 'email_variables_list');
/**
* This gets called to replace the variable in the email with an actual value
* #param $var - Modify this: array key is the variable name, value is the replacement
*/
function register_variable_replacements($var, $email_data, $queue_item, $email){
global $wpdb;
// Look up UID from order number
$orderNumber = $var->get_variables()['order_number'];
$sql = " //Your sql statement here...//";
$results = $wpdb->get_results($sql);
$UID = $results[0]->meta_value;
$variables = array(
'uid' => $UID
);
$var->register($variables);
}
function email_variables_list($email)
{
global $woocommerce;
?>
<li class="var hideable var_subscriptions">
<strong>{uid}</strong>
<img class="help_tip" title="<?php _e('Order UID', 'follow_up_emails'); ?>" src="<?php echo $woocommerce->plugin_url(); ?>/assets/images/help.png" width="16" height="16"/>
</li>
<?php
}
Now on your follow up email plugin, you can use {uid} as a variable and this will be replaced with the correct value on every email.
I though the best way was to use short code, but then i discovered this filter and i think this is the best way to handle this. This code is also pretty flexible and you can add as many variables as you want.
Hope this can help someone.

WooCommerce Registration Shortcode - Error messages problems

I am currently creating a widget to display the registration form on a WordPress website that uses WooCommerce. For now, I only have 3 basic fields which are email, password, repeat password. I'm looking forward to add more WooCommerce fields, but want to solve that problem before jumping to the next step.
I'm having some problems with the messages output (wrong password, account already exists, etc).
I searched on the web and there was no shortcode already built for WooCommerce registration, beside their registration page. So I went ahead and created a shortcode, with a template part.
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
This is a snippet I use to get the template part inside a variable, since the default function would "echo" the content instead of "returning" it.
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
So, the problem is, when I call woocommerce_show_messages or $woocommerce->show_messages(); from my template part, nothing is showing, or if it is, it shows at the top of the page.
I did try to put the calls inside my shortcode function:
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$woocommerce->show_messages();
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
Doing so, the message output inside the <head> tag, which is not what I want.
I tried to do the same trick with ob_start(), ob_get_contents() and ob_clean() but nothing would show. The variable would be empty.
I also did try to hook the woocommerce_show_messages to an action as saw in the core:
add_action( 'woocommerce_before_shop_loop', 'woocommerce_show_messages', 10 );
For something like:
add_action( 'before_registration_form', 'woocommerce_show_messages');
And I added this in my template-part:
<?php do_action('before_registration_form'); ?>
But I still can't manage to get the error messages show inside the box. It would always be inserted in the <head>
I will share final solution when everything is done.
Thanks for your time,
Julien
I finally got this working by hooking a custom function to an action which is called in my header.php
I guess hooking functions inside template part does not work as intended.
In header.php, I got this:
do_action('theme_after_header');
And here's the hooked function. Works perfectly.
function theme_show_messages(){
woocommerce_show_messages();
}
add_action('theme_after_header', 'theme_show_messages');
However, I will look into 'unhooking' the original show message function since it might show twice. Need to test some more ;)
You can also just use the [woocommerce_messages] shortcode in your template where you want it displayed
Replying to a bit of an old question, but you can also try the following:
$message = apply_filters( 'woocommerce_my_account_message', '' );
if ( ! empty( $message ) ) {
wc_add_notice( $message );
}

Wordpress Plugin: Show html only on standard page and not in admin area

I'm writing a plugin and I need to display a piece of text in the WP page, but not in the admin area. How can I do so?
I tried this in the construct:
add_action( 'init', array( $this, 'initPage' ) )
and then:
public function initPage() {
echo 'hello';
}
but the text is displayed also in the admin area. Is there a way to do this? It would be the opposite of the action admin_init I assume.
Proper way to handle it: is_admin()
http://codex.wordpress.org/Function_Reference/is_admin
if(is_admin()) { // do nothing } else {
// function you want to execute.
}
I solved this by adding it to a shortcode action. Like this:
add_shortcode( 'myPlugin', array( $this, 'shortcode' ) );
and:
public function shortcode( $atts ) {
return 'hello';
}
With the above code, 'hello' will only display on the front-end. Not sure if that's the cleaner way to do it, but does the job.
There is no "front-end-only" version of init, however you probably don't want to be doing any output at the init action anyway.
What exactly are you trying to do? Usually, you use an action hook for specific types of things, and causing output very early at something like "init" is rare and weird.

Resources