WP: overriding recent post widget class - wordpress

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;
}

Related

How do I add my class the earliest possible?

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.

Remove description meta box from custom taxonomy

I'm trying to remove these areas from one of my custom taxonomies.
I've built them using the two plugins: Custom Post Types UI (to add them) and Advanced Custom Fields (to add fields to the taxonomy).
I can't see anything in the plugin settings to remove these things, but I'm not sure if I'm missing something.
I'm assuming I might need to add a function to the functions.php file. I've seen that hiding things using jQuery is a possibility, but I hear that this might show it initially on load and then hide it, so id like to learn how to doit properly.
I managed to get there with the link above and help from another website.
This function removed the two instances of the 'description' box:
function hide_description_row() {
echo "<style> .term-description-wrap { display:none; } </style>";
}
add_action( "measure_sectors_edit_form", 'hide_description_row');
add_action( "measure_sectors_add_form", 'hide_description_row');
and this one removed the column from the right hand side:
add_filter('manage_edit-measure_sectors_columns', function ( $columns ) {
if( isset( $columns['description'] ) )
unset( $columns['description'] );
return $columns;
});
To use with other taxonomies, just replace 'measure_sectors' with your own taxonomy slug

Wordpress Hook the_content Within Widget

I am writing a widget that list the headings in the post and then created hash links and edits the HTML to reflect that. I've got the list widget content figured out and I just need to edit the_content, i've tried to add a filter for the method that returns the updated code but it's not working.
What would be the best way to do this? My class is called post_headings_widget and the edited HTML content is stored within $this->the_content.
I was hoping I could do this within the widget class
public
function edited_content() {
return $this->the_content;
}
and then to edit the content output here
add_filter( 'the_content', [ 'post_headings_widget', 'edited_content' ] );
It calls the class method fine but i'm not sure exactly how it works so i'm guessing it called the method directly without calling the constructors etc?
I have also tried to just create a filter from within the widget() method but that did not work either, heres what I tried:
add_filter( 'the_content', function() {
return 'test';
} );
Any ideas on a solution?
You have to pass the_content as a parameter in your filter function/callback.
Check the Wordpress docs: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
On widgets you need to bind on widget_text
add_filter('widget_text', 'se24265_my_function');
function se24265_my_function( $content )
{
# replace code here on widget $content
return $content;
}

Override Woocommerce Payment Gateway Templates

I have made a change to the eBay payment gateway file as I want to change the PayPal button text to say something else. I figured this was my best solution as I am not familiar with doing my own hooks/filters.
I have checked this amended template file works - it does. However, when I load this to my theme files it does not override.
I have tried:
main theme directory
theme/woocommerce/file
theme/woocommerce/includes/gateways/paypal/file
None of these work... can anyone help me out?
Thanks in advance :-)
It seems there is no override solution to your question. But you can add a brand new payment gateway
simply extending the WC_Payment_Gateway class, in other words by adding another payment gateway.
Step 1
You can duplicate the file:
plugins/woocommerce/includes/gateways/class-wc-gateway-paypal.php
in your directory theme, change its name for convenience and include it in functions.php:
/* Custom gateway class */
require( get_template_directory() . '/path/to/class-wc-gateway-paypal-custom.php' );
Step 2
This file holds the WC_Gateway_Paypal class which extends WC_Payment_Gateway. You can edit this file for your customizations.
Remember to change the name of the extender class:
class WC_Gateway_Paypal_Custom extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'paypal';
$this->icon = apply_filters( 'woocommerce_paypal_icon', WC()->plugin_url() . '/assets/images/icons/paypal.png' );
$this->has_fields = false;
// Change the text in the way you like it
$this->order_button_text = __( 'Proceed to PayPal', 'woocommerce' );
$this->liveurl = 'https://www.paypal.com/cgi-bin/webscr';
$this->testurl = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
$this->method_title = __( 'PayPal', 'woocommerce' );
$this->notify_url = WC()->api_request_url( 'WC_Gateway_Paypal' );
}
//other payment gateway stuff
}
Give it a try, let us know if you get stuck! : )
UPDATE 06/13/2014
It's also useful to know that there's a filter that allows you to change the paypal image, so:
function paypal_checkout_icon() {
// pls return the new logo/image URL here
return 'http://www.url.to/your/new/logo.png';
}
add_filter( 'woocommerce_paypal_icon', 'paypal_checkout_icon' );

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