Trying to extend a Wordpress plugin, need to hook a function - wordpress

I am trying to add some site-specific functionality to the Wordpress plugin Appointments+. When a user is making an appointment, they select certain services and enter information into some fields for confirmation.
What I want to do is add some code so the extra information fields are related to which service they choose. For example, if they choose Service A, they have to fill in some information and those fields are validated. But if they choose Service B, hide the fields and skip the validation.
Here is an example of the original code:
function post_confirmation() {
...some extra stuff
$values = explode( ":", $_POST["value"] );
$location = $values[0];
$service = $values[1];
do_action('app-additional_fields-validate');
...more stuff
}
Here is the kind of thing I want to do, but not alter this original plugin file:
function post_confirmation() {
...some extra stuff
$values = explode( ":", $_POST["value"] );
$location = $values[0];
$service = $values[1];
if (($service == 1) || ($service == 3)) {
do_action('app-additional_fields-validate');
}
...more stuff
}
I admit that I am new to using hooks and filters. I want to disable/enable that action based on a variable in the function. How can I do that with another hook/filter?
Thank you.

Unhook the hook used for calling post_confirmation and override this function from your functions.php like
remove_action( 'current_action_hook', 'post_confirmation' );
add_action( 'current_Action_hook', 'post_custom_confirmation' );
function post_custom_confirmation(){
...some extra stuff
$values = explode( ":", $_POST["value"] );
$location = $values[0];
$service = $values[1];
if (($service == 1) || ($service == 3)) {
do_action('app-additional_fields-validate');
}
...more stuff
}
I think this should work, I have used this way during a woocommerce plugin development before nearly 2 months below is working code example , so that if you need any modification on code above, you can take reference .
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
add_action( 'woocommerce_checkout_order_review', 'pk_order_review', 10, 1 );
function pk_order_review($template) {
global $woocommerce;
$template = wc_get_template ( 'checkout/review-order.php', FALSE, FALSE, untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/');
return $template;
exit;
}
Another way you can do it is
add_action( 'app-additional_fields-validate', 'foo' );
function foo(){
if (($service == 1) || ($service == 3)) {
/* Your stuffs */
}
}

This is actually part answer, part new question. If that is possible to do here.
So, I finally found a way to affect the desired function.
The function I was trying to modify is contained in a class so, I did something like this:
class my_Check extends Appointments {
function __construct() {
$this->unregister_parent_hook();
add_action( 'wp_ajax_post_confirmation', array( $this, 'post_confirmation' ) );
add_action( 'wp_ajax_nopriv_post_confirmation', array( $this, 'post_confirmation' ) );
}
function unregister_parent_hook() {
global $appointments; //this was the object created with the parent class
remove_action( 'wp_ajax_post_confirmation', array( $appointments, 'post_confirmation' ) );
remove_action( 'wp_ajax_nopriv_post_confirmation', array( $appointments, 'post_confirmation' ) );
}
function post_confirmation() {
...do the stuff with my mods...
}
}
$new_Check = new my_Check();
Only, I have a new problem now. The parent class does a lot more in the __construct() (many 'add_action()'s, etc.). And $this is populated with a lot more data. The problem is, these other things and data do not seem to be carrying over into the child class. I tried adding a parent::__construct() in the child's __construct() function, but that did not seem to work.
The code with my mods works except for things that need more data in the $this carried over from the parent class.
How can I maintain all the parent class's variable, functions, hooks & filters, etc. into the child class?

Related

How do I replace the OOTB woo customer completed order email?

In a plugin I'm working on, I'm trying to replace the OOTB customer-completed-order email with a template in the plugin
Constructor:
define( 'BKF_WC_EMAIL_PATH', plugin_dir_path( __FILE__ ) );
add_filter('wc_get_template', array($this, 'bkf_customer_completed_order_template'), PHP_INT_MAX, 5);
Function:
function bkf_customer_completed_order_template($template, $template_name, $args, $template_path, $default_path) {
if( $template_name == 'emails/customer-completed-order.php' ) {
$template = trailingslashit(BKF_WC_EMAIL_PATH) . 'templates/' . $template_name;
return $template;
}
}
note the template is still pulling the default woo one
Any thoughts/ideas are welcome!
Worked it out!
Instead of the method used in my original question, here's what worked for me:
I created a new class (similar to woocommerce/includes/emails/class-wc-email-customer-completed-order.php) - for demo purposes we'll call it My_Custom_Class
I then called like so in my constructor for the parent class I was working on:
add_action('woocommerce_email_classes', array( $this, 'bk_register_email' ), PHP_INT_MAX, 1 );
And added this function:
public function bk_register_email( $emails ) {
require_once 'emails/my-custom-class.php';
$emails['WC_Email_Customer_Completed_Order'] = new My_Custom_Class();
return $emails;
}

Use Wordpress shortcode function to render Gutenberg block, sending the attributes as parameters

I have a shortcode that generates a gallery, given the gallery ID.
function rb_scroll_gallery_shortcode( $atts, $content ) {
$a = shortcode_atts( array(
'id' => -1,
), $atts );
$gallery_ID = $a['id'];
$output = '';
if($gallery_ID != -1){
ob_start();
$gallery = new RB_Scroll_Gallery($gallery_ID);
$gallery->render();
$output = ob_get_clean();
}
return $output;
}
add_shortcode( 'rb_scroll_gallery', 'rb_scroll_gallery_shortcode' );
Now, I've made a Gutenberg block that works perfectly in the editor. You can select a gallery and it will save. However, I dont want to repeat code and have the html in the save property and in the php code.
So I was wondering if there is a way to use that same rb_scroll_gallery_shortcode function to render the block in the front end.
I've seen that you can use register_block_type and set the render_callback to rb_scroll_gallery_shortcode, but I need the ID selected in the block to send it to the function in the $atts array
//This uses the shortcode funtion, but doesn't gives the gallery ID
register_block_type( 'cgb/block-rb-scroll-gallery-block', array(
'render_callback' => 'rb_scroll_gallery_shortcode',
) );
You can try to Convert a Shortcode to a Gutenberg Block and after use in your theme,
Registering the Dynamic Block Callback in PHP
/**
* Register the GitHub Gist shortcode
*/
function gggb_init() {
add_shortcode( 'github-gist', 'gggb_render_shortcode' );
register_block_type( 'github-gist-gutenberg-block/github-gist', array(
'render_callback' => 'gggb_render_shortcode',
) );
}
add_action( 'init', 'gggb_init' );
When your block is rendered on the frontend, it will be processed by your render callback:
function gggb_render_shortcode( $atts ) {
if ( empty( $atts['url'] )
|| 'gist.github.com' !== parse_url( $atts['url'], PHP_URL_HOST ) ) {
return '';
}
return sprintf(
'<script src="%s"></script>',
esc_url( rtrim( $atts['url'], '/' ) . '.js' )
);
}
**Note:** this render callback is intentionally different than the Gutenberg block’s edit callback. Our preference is to use GitHub’s provided JavaScript embed code because this lets GitHub change the embed’s behavior at a future date without requiring a developer to make changes.
Refer link for get more information, https://pantheon.io/blog/how-convert-shortcode-gutenberg-block
I've found out the little thing that messed up my code. The problem wasn't that the render_callback() wasn't getting any attributes (though it wasn't), but it was that the editor wasn't saving them because I forgot to remove some testing data from the attribute galleryID
In the registerBlockType:
The save() method should return null.
The attribute should not have a selector data, since it is used to find the value on the markup return by the save(), wich in this case returns null. I've forgot to remove this data, thats why the attribute wasn't being saved.
attributes: {
galleryID: {
type: 'string',
//This data should only be set if the value can be found in the markup return by save().
//Since save() returns null, we let it out
/*source: 'attribute',
/*attribute: 'data-gallery-id',
/*selector: '.rb-scroll-gallery-holder',*/
},
}

How to Display Custom meta field value insted of title on custon post list table?

I have created a custom post type "cinfo" and removed title and editor form the edit page. With the help of this code. Also displayed some custom meta fields which are relevant to my plugin.
function remove_box(){
remove_post_type_support('cinfo', 'title');
remove_post_type_support('cinfo', 'editor');
}
add_action("admin_init", "remove_box");
It looks something like this.
Now when i see the list page I still see the title with edit, view and delete button beneath it. which I don't want because the title field doesn't exist in the edit page So it looks a bit irrelevant in the listing page. Instead of that I tried to display the custom meta field "email" but I was only able to change the heading of the column. which looks something like this.
I just did some research and found one action and filter but they still didn't seems to be much of a help to me. Still for the better view of the problem. Also I tried to use a plugin Post List View Count but it also didn't accomplish my purpose. I hope You understand what I basically want to do. Thanks for your time to read my question.
add_filter('manage_cinfo_posts_columns', 'bs_cinfo_table_head');
function bs_cinfo_table_head( $defaults ) {
$defaults['title'] = 'Email';
return $defaults;
}
add_action( 'manage_cinfo_posts_custom_column', 'card_cinfo_table_content', 10, 2 );
function card_cinfo_table_content( $column_name, $post_id ) {
if ($column_name == 'title') {
echo "its working";
}
}
The action manage_cinfo_posts_custom_column will never be true. It's better to remove the defaults on manage_cinfo_posts_columns and do the regular custom stuff in the other filter.
I tried this with a fast class to test all together inside my setup:
class SO23467344
{
private $cpt = 'portfolio';
private $custom_field = 'video';
public function __construct()
{
add_filter( "manage_edit-{$this->cpt}_columns", array( $this, 'column_register' ), 20, 1 );
add_action( "manage_{$this->cpt}_posts_custom_column", array( $this, 'column_display' ), 20, 2 );
add_action( "admin_init", array( $this, "remove_box" ) );
}
function column_register( $columns )
{
$columns['my-col'] = 'My column';
# Remove default columns
unset( $columns['title'], $columns['categories'], $columns['comments'], $columns['date'] );
return $columns;
}
function column_display( $column_name, $post_id )
{
if ( 'my-col' != $column_name )
return;
if ( $field = get_post_meta( $post_id, $this->custom_field, true ) )
echo '<br/><strong style="color:#0f0;font-size:4em"> • </strong>';
}
function remove_box(){
remove_post_type_support( $this->cpt, 'title' );
remove_post_type_support( $this->cpt, 'editor' );
}
}
new SO23467344;

Override a function with child themes functions.php

As the title reads I'm trying to modify a function called by a parent theme in my child, I know that the child theme is set to be loaded beforehand so I'm curious if this is even possible?
My parent theme has a function called ajax_search_box() that I'd like to modify a query in, and I'd rather not modify the parent theme files in case I need to update it down the road.. what would be the best way to do this?
Also, for bonus points, how would I go about doing this with a widget as well? Thanks in advance!
function SearchFilter($query) {
// If 's' request variable is set but empty
if (isset($_GET['s']) && empty($_GET['s']) && $query->is_main_query()){
$query->is_search = true;
$query->is_home = false;
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
function ajax_search_box() {
if (isset($_GET["q"]))
{
$q = $_GET["q"];
global $wpdb;
$q = mysql_real_escape_string($q);
$q = $wpdb->escape($q);
$query = array(
'post_status' => 'publish',
'order' => 'DESC',
's' => $q
);
$get_posts = new WP_Query;
$posts = $get_posts->query( $query );
// Check if any posts were found.
if ( ! $get_posts->post_count )
die();
//Create an array with the results
foreach ( $posts as $post )
echo $post->post_title . "|" . $post->ID . "\n";
}
die();
}
// creating Ajax call for WordPress
add_action( 'wp_ajax_nopriv_ajax_search_box', 'ajax_search_box' );
add_action( 'wp_ajax_ajax_search_box', 'ajax_search_box' );
the parent theme needs to check if(function_exists('ajax_search_box')) and if it doesn't exist then it will declare it.
If the parent theme checks to see if the function exists, then you can declare it first and have it do what you want.
If the parent theme does not check, get in touch with the theme author to see if they will throw that change in for the next update....and code it yourself too. That way when the theme updates then you will still be good to go.
Break free of functions.php, write your own plugin.
<?php
/**
* Plugin Name: Manipulate the Parent
* Requires: PHP5.3+
*/
add_action( 'after_setup_theme', function()
{
remove_filter( 'pre_get_posts','SearchFilter' );
// now add your own filter
add_filter( 'pre_get_posts', 'your_callback_for_your_filter' );
});
function your_callback_for_your_filter()
{
// do stuff
}

WordPress plugin and wp_head

I've re-edited this question: is possible to before to show the output in point 2 pass a variable to global color (point 3) like a global variable or something?
class myclass
{
public function init()
{
global $shortcode_tags;
add_shortcode( MYSHORTCODE, array( 'myclass', 'shortcode' ) );
// * point 1
return;
}
public function shortcode( )
{
// *point2
}
function globalcolor($color)
{
echo '<style>body{color:' .$color . '}</style>' . "\n";
// * point 3
}
}
add_action( 'wphead', array( 'myclass', 'globalcolor' ) );
add_action( 'init', array( 'myclass', 'init' ) );
PS. right now im reading about custom fields.enter code here
do_action() is called by WordPress, you want add_action().
The action init comes way too early. You call the class now even for the backend, for AJAX requests etc. Use the hook template_redirect which is called on the frontend only.
You cannot send the color value the way you tried. See the sample code for a working example.
Sample code:
class My_Plugin {
/**
* Container for your color value.
* #var string
*/
static $color;
public static function init()
{
// Set the color value as a class member.
self::$color = '#345';
// Class methods are addressed with an array of the object or the
// class name and the function name.
add_action( 'wp_head', array ( __CLASS__, 'print_color' ) );
}
public static function print_color()
{
// In action you have to print/echo to get an output.
print '<style>body{color:' . self::$color . '}</style>';
}
}
add_action( 'template_redirect', array ( 'My_Plugin', 'init' ) );
I strongly recommend https://wordpress.stackexchange.com/ to ask more questions on WordPress. :)

Resources