Are widget(), update() and form() pre-created functions in Wordpress? - wordpress

On the Wordpress Codex page for 'register widget' there is basic example code given for registering a widget via your plugin:-
class MyNewWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, 'My New Widget Title' );
}
function widget( $args, $instance ) {
// Widget output
}
function update( $new_instance, $old_instance ) {
// Save widget options
}
function form( $instance ) {
// Output admin widget options form
}
}
function myplugin_register_widgets() {
register_widget( 'MyNewWidget' );
}
add_action( 'widgets_init', 'myplugin_register_widgets' );
In this code, as you can see the three functions I mentioned are provided. I want to know if I can change their names or are they pre-created Wordpress functions?

You can name your members in MyNewWidget whatever you like, but the point of extending WP_Widget is that WP_Widget's methods are all available to MyNewWidget but you can override them by writing a method of the same name. This may help explain.

Related

How to use add_action from outside a class

I am learning about wordpress code and have read a lot of posts about add_action, but haven't found one yet that fits my situation. I want to make use of a hook that exists in another WP plugin. In rough outline, it looks like this:
// This part is not my code
class CDH {
function do_stuff() {
// important stuff
do_action( 'hook_i_want_to_use', $parameter );
// more stuff
}
global $cdh;
$cdh = new CDH();
}
// My first attempt code:
add_action( 'hook_i_want_to_use', 'my_function', 10, 1 );
function my_function( $parameter ) {
echo $parameter;
}
// My second attempt code:
class my_CDH extends CDH {
public function __construct() {
add_action( 'hook_i_want_to_use', array( $this, 'my_function' ), 10, 1 );
}
function my_function( $parameter ) {
echo $parameter;
}
}
In both attempts, my_function() is never called. How do I hook on to the do_action in the instance $cdh?

What is the proper way to 'externalize' TimberSite methods?

I'm using Timber plugin, and I would like to have separate files for registering CPTs, taxonomies etc. I have a lot of those and do not want to clutter up the functions.php file. I know I can just use require_once(), but I'm not sure if it is the proper way.
class StarterSite extends Timber\Site {
public function __construct() {
...
add_action( 'init', array( $this, 'register_post_types' ) );
...
parent::__construct();
}
public function register_post_types() {
// Proper way to have this data in a different file?
}
}

How can I extend the WooCommerce WC_Cart class and override select methods?

I can't even get started; an empty class save for the construct which calls the parent construct, when I try adding anything to the cart, ajax says ok view cart, but the cart always remains empty. Same when I copy the entire WC_Cart contents into my class. I have a plugin with a class that sets $GLOBALS['woocommerce']->cart = new WC_Custom_Cart(); once wp_loaded.
class WooCustom {
public function __construct() {
add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
}
function wp_loaded() {
self::woocustom_cart_init();
}
private function woocustom_cart_init() {
if ( is_main_site( get_current_blog_id() ) ) {
if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
require_once( 'classes/wc-custom-cart.php' );
$GLOBALS['woocommerce']->cart = new WC_Custom_Cart();
}
}
}
}
class WC_Custom_Cart extends WC_Cart {
public function __construct() {
parent::__construct();
}
}
UPDATE: Thank you all for your consideration and I apologize for my vague pleading and inquiry.
What I need first of all is in the cart to simply eradicate all pricing/fee totals. (OK to display custom fields Cost and MSRP, but no charge.)
Also variable products need to be able to be added to the cart like simple products, sans variations selection, because the entire product is to be imported with all of its variations.

How to extend a custom widget in wordpress?

I am talking about making a widget which extends a child widget of WP_Widget.
Using this as reference: https://wordpress.stackexchange.com/questions/101438/how-to-extend-a-wp-widget-twice
Example:
My_WidgetBase extends WP_Widget
My_Widget extends My_WidgetBase
I want to know how to get My_Widget to show up with my other widgets (it is not currently). I have gotten My_WidgetBase to work.
This is important for my framework. I understand that widget(), update(), and form() must be overridden, but I could not find anything about making a grandchild of WP_Widget anywhere.
Example:
class My_WidgetBase extends WP_Widget {
public function __construct($id = 'my-widget-base', $desc = 'My WidgetBase', $opts = array()) {
$widget_ops = array();
parent::__construct( $id, $desc, $widget_ops );
}
public function widget( $args, $instance ) {
die('function WP_Widget::widget() must be over-ridden in a sub-class.');
}
public function update( $new_instance, $old_instance ) {
return $new_instance;
}
public function form( $instance ) {
echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
return 'noform';
}
}
function RegisterMyWidgets() {
register_widget('My_WidgetBase');
}
add_action( 'widgets_init', 'RegisterMyWidgets' );
Solved digging further into this thread: https://wordpress.stackexchange.com/questions/101438/how-to-extend-a-wp-widget-twice
The problem was not with my implementation of the classes, but how I included the parent in my framework. I was not working with both child and grandchild in the same php file, but two separate files with different directories.
Solving this problem was a matter of using require_once() correctly in the directory of my grandchild class.

(WP) Override WP Plugin Public Function

I want override WP public function in plugin files. I want to place it on my theme functions.php file and don't want to directly edit plugin files because I want this function only applicable when using this theme only.
How can I override this public function?
class WP_Job_Manager_Post_Types {
public function application_details_email( $apply ) {
get_job_manager_template( 'job-application-email.php', array( 'apply' => $apply ) );
}
}
You could instead try the job_manager_locate_template filter:
add_filter( 'job_manager_locate_template',
function( $template, $template_name, $template_path )
{
if( 'job-application-email.php' === $template_name )
{
// modify $template;
}
return $template;
}
, 10, 3 );
to modify the template path.

Resources