In WooComerce file class-wc-form-handler.php
I wanted to override in my child theme just one line where the variable $redirect.
What is the best practice in this case ?
class-wc-form-handler
public function process_registration() {
if ( ! empty( $_POST['register'] ) ) {
...
// Redirect
if ( wp_get_referer() ) {
$redirect = esc_url( wp_get_referer() );
} else {
//$redirect = esc_url( get_permalink( wc_get_page_id( 'myaccount' ) ) );
$redirect = home_url() . $_SERVER["REQUEST_URI"];
}
wp_redirect( apply_filters( 'woocommerce_registration_redirect', $redirect ) );
exit;
}
You don't need to override any method. As you can see on line where wp_redirect there is filter hook. Read more about apply_filters.
You can add your own custom filter to the tag woocommerce_registration_redirect using add_filter.
Heres how you can do it:
add_filter( 'woocommerce_registration_redirect', 'my_custom_redirect' );
function my_custom_redirect()
{
return esc_url( get_permalink( wc_get_page_id( 'myaccount' ) ) );
}
Place the above code in your child theme functions.php file.
Related
I have a url
example.com/sample-page/?amount=567
It initially worked ok, it redirects to:
example.com/sample-page/567
But after few days, i noticed its not working any more.
Its now redirecting to post without query var or get var
What could be the reason?? Is there any solution??
I used below codes in my functions.php file:
& I already flushed permalinks.
I am using custom post type
I have removed custom post type base from url using a plugin.
UPDATE
I just noticed Its due to empty post id/pagename.
So now, Question is how to get page name of current post...inside init function dynamically??
//https://developer.wordpress.org/reference/functions/wp_redirect/#comment-4109
add_action( 'init', function() {
global $wp;
$pagename = $wp->request;
add_rewrite_rule( ''.$pagename.'/([0-9]+)[/]?$', 'index.php?pagename='.$pagename.'&amount=$matches[1]', 'top' );
} );
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'amount';
return $query_vars;
} );
function wpb_change_search_url() {
global $wp;
if ( ! empty( $_GET['amount'] ) ) {
wp_redirect( get_permalink( home_url( $wp->request ) ) . urlencode( get_query_var( 'amount' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
I want to redirect to custom page after purchase on woocommerce code below:
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( get_page_by_title( About )->ID );
exit;
}
}
It redirects to 'order-received' page which does not exist.
You have forgot th little "" around the title and you should need to use get_permalink() function too this way:
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( get_permalink( get_page_by_title( "About" )->ID ) );
exit;
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
I have tested it and this should work now
I'm trying to change the "read more" link in a child theme.
From the codex, I've added the following to the functions.php file of the parent theme (twentyseventeen)
// Replaces the excerpt "Read More" text by a link
function new_excerpt_more($more) {
global $post;
return ' <a class="moretag" href="'.
get_permalink($post->ID) . '"> Read the full article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
...and it does what I'd like it to do.
However, I don't want it to be wiped out if the theme is ever updated.
So I've created a child-theme.
However when I add the code to the child theme functions.php file, it does not work.
My child-theme functions.php file looks like this:
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
if ( !function_exists( 'child_theme_configurator_css' ) ):
function child_theme_configurator_css() {
wp_enqueue_style( 'chld_thm_cfg_separate', trailingslashit( get_stylesheet_directory_uri() ) . 'ctc-style.css', array( 'chld_thm_cfg_parent','twentyseventeen-style' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css' );
function my_scripts_method() {
wp_enqueue_script(
'custom-script',
get_stylesheet_directory_uri() . '/custom_script.js',
array( 'jquery' )
);
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
// END ENQUEUE PARENT ACTION
// Replaces the excerpt "Read More" text by a link
function new_excerpt_more($more) {
global $post;
return ' <a class="moretag" href="'. get_permalink($post->ID) . '"> Read the full article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
I was able to get this to work with some information from this question:
Wordpress functions.php child theme [Resolved]
In my child-theme functions PHP, I checked to see if the original twentyseventeen_excerpt_more() function has executed, and then give my alternative readmore() function a higher priority. With the higher priority (15), add_filter( 'excerpt_more', 'readmore', 15 ); my function runs first, and the original read more function is ignored in the parent theme.
This is what the function looks like in my child-theme functions.php file:
if ( ! function_exists ( 'twentyseventeen_excerpt_more' ) ) {
function readmore( $link ) {
if ( is_admin() ) {
return $link;
}
$link = sprintf( '<p class="link-more">%2$s</p>',
esc_url( get_permalink( get_the_ID() ) ),
/* translators: %s: Name of current post */
sprintf( __( '[Read More...]<span class="screen-reader-text"> "%s"</span>', 'twentyseventeen' ), get_the_title( get_the_ID() ) )
);
return ' … ' . $link;
}
add_filter( 'excerpt_more', 'readmore', 15 );
}
BTW, the above could also be inserted into a plugin .php file if you would prefer.
I need to change the default rss url of my website:
from example.com/feed to example.com/MyfeedName
Update:
what i tried so far is to create another Url feed but i need to remove firstexample.com/feed:
add_action( 'init', function()
{
add_feed( 'secretfeed', 'do_feed_rss2' );
});
add_action( 'pre_get_posts', function( \WP_Query $q )
{
if( $q->is_feed( 'secretfeed' ) )
add_filter( 'option_rss_use_excerpt', '__return_false' );
} );
do you have any idea how to just edit example.com/feed or how to delete it without losing rss functions ?
I found my answer here :
https://wordpress.stackexchange.com/a/214883/71314
function remove_feed( $feedname ) {
global $wp_rewrite;
if ( in_array( $feedname, $wp_rewrite->feeds ) ) {
$wp_rewrite->feeds = array_diff( $wp_rewrite->feeds, array( $feedname ) );
}
$hook = 'do_feed_' . $feedname;
// Remove default function hook
remove_all_actions( $hook );
add_action( $hook, $hook );
return $hook;
}
Usage:
remove_feed( 'feed' );
I would like to add a custom icon to my payment gateway. I have read the WOO gateway API and have zero help. This is my code below. please help me find a functional way to include the icon so I have an icon on my front end. Thanks
<?php if ( ! defined( 'ABSPATH' ) ) { exit; }
add_filter( 'woocommerce_payment_gateways', 'init_wpuw_gateway' );
function init_wpuw_gateway ( $methods )
{
$methods[] = 'WC_Gateway_WPUW';
return $methods;
}
if( class_exists('WC_Payment_Gateway') ):
class WC_Gateway_WPUW extends WC_Payment_Gateway {
/**
* Constructor for the gateway.
*/
public function __construct() {
$plugin_dir = plugin_dir_url(__FILE__);
$this->id = 'wpuw';
//If you want to show an image next to the gateway’s name on the frontend, enter a URL to an image.
$this->icon = apply_filters( 'woocommerce_gateway_icon', ''.$plugin_dir.'/assets/paysecure.png' );
$this->method_title = __( 'User Wallet', 'woocommerce' );
$this->method_description = __( 'Have your customers pay with their user wallet balance.', 'woocommerce' );
$this->has_fields = false;
Try with backslash instead of slash without concatenating the initial empty string with the variable $plugin_dir
$this->icon = apply_filters( 'woocommerce_gateway_icon', $plugin_dir.'\assets\paysecure.png' );
Using the WooCommerce filter woocommerce_gateway_icon you can add the icon to the payment gateway this way:
/**
* Add Custom Icon
*/
function custom_gateway_icon( $icon, $id ) {
if ( $id === 'custom' ) {
return '<img src="' . plugins_url( 'img/custom.png', __FILE__ ) . '" > ';
} else {
return $icon;
}
}
add_filter( 'woocommerce_gateway_icon', 'custom_gateway_icon', 10, 2 );
I would suggest woocommerce_available_payment_gateways filter.
/**
Add Custom Icon For Cash On Delivery
**/
function cod_gateway_icon( $gateways ) {
if ( isset( $gateways['cod'] ) ) {
$gateways['cod']->icon = get_stylesheet_directory_uri() . '/images/cod.png';
}
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'cod_gateway_icon' );
try this:
$this->icon = trailingslashit( WP_PLUGIN_URL ) . plugin_basename( dirname( __FILE__ ) ) . '/assets/paysecure.png';