Woocommerce Filter to update tax_class - wordpress

This filter work well.
<pre><code>
function wc_diff_taxes_for_ajax( $tax_class, $product ) {
$tax_class = 'Zero Product';
return $tax_class;
}
add_filter( 'woocommerce_product_tax_class', 'wc_diff_taxes_for_ajax', 1, 2 );
</code></pre>
I try to fire filter inside my other function but always have an error...
The Ajax part call well and get response. So i don't put it here.
<pre><code>
function manage_taxes_aliquota() {
if(isset($_POST['tax_class'])) {
<--What i PUT HERE TO FIRE THE FILTER AND PASS NEW $tax_class-->
}
echo 'ok';
exit;
}
add_action('wp_ajax_nopriv_manage-taxes-aliquota', 'manage_taxes_aliquota');
add_action('wp_ajax_manage-taxes-aliquota', 'manage_taxes_aliquota');
</code></pre>
Regards.

Related

Wordpress: add custom params to all URLS

I have an addition to url e.g. /products/myproduct/?v=iphone-x/transparent/*/Green
So what I need is for wordpress to add the ?v=iphone-x/transparent/*/Green to all links on the page (only '<a href="">'s, no 'img src=""' or others)
I have managed to do that, but it's a little "dirty". Is there any neat function to add the parameter to all links?
The code I have is as follows:
function callback($buffer) {
// modify buffer here, and then return the updated code
$temp = explode('href="', $buffer);
$buffer = $temp[0];
array_shift($temp);
foreach($temp as $t){
$tt = explode('"', $t, 2);
$buffer .= 'href="'.$tt[0].'?v='.$_GET['v'].'"'.$tt[1];
}
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { ob_end_flush(); }
add_action('wp_head', 'buffer_start');
add_action('wp_footer', 'buffer_end');
One way you can achieve this is to hook into "the_content" filter. By using regexp with preg_replace_callback function you can get decent results.
function add_para( $content ) {
$content = preg_replace_callback(
"/href=(?>'|\")([^\"']+)(?>'|\")/",
function($m) {
print_r($m);
return "href='".$m[1]."/additional-param'";
},
$content);
return $content;
}
add_filter( 'the_content', 'add_para', 0 );
However, you might run into some issues particularly if your content is not formatted probably (extra spaces, missing tags .. etc).
So the alternative is either to us a JS approach (jQuery for example), or using PHP DOM parser like: PHP Simple HTML DOM Parser

Wordpress - How to set title dynamically based on content?

I try to set custom titles using data from the content. Those are not pages nor posts - plugin (chaturbate) that I'm fixing somehow process urls like /lala-fafa without having any entries in the posts table.
Here is my code
add_filter( 'the_title', 'chfix_alter_title', 9999);
function chfix_alter_title($title, $id=null)
{
global $chfix_values;
return "$chfix_values[Name] $chfix_values[Age]...";
}
add_filter( 'the_content', 'chfix_doit', 9999 );
function chfix_doit($content)
{
global $chfix_names, $chfix_values;
if (!is_admin())
{
foreach($chfix_names as $name)
{
// parsing content
$chfix_values = chfix_get_field($content, $name);
}
}
return $content;
}
The problem is that the_title filter is called AFTER the_content filter. I also tried wpseo_title, widget_title with no luck. Is any possibility to make what I want?

template_include stop working on woocommerce update

The following code is to override the Product Details page template, It was working since the last update on WooCommerce. Can anyone help me out on this, thanks in advance.
add_filter('template_include', 'wpautomate_plugin_templates');
function wpautomate_plugin_templates( $template )
{
$plugin_path='';
$reflector = new ReflectionClass('Ze_Single_Product_Layout');
$file_name=plugin_dir_path($reflector->getFileName());
$plugin_path=$file_name;
$post_types = array('product');
$template_id=get_post_meta( get_the_ID(), '_product_layout', true );
if (is_singular('product') && !empty($template_id))
{
//render custom template for single product
$template = $plugin_path . 'template/woo-single-page.php';
}
return $template;
}//end of function
You need to call this filter
add_filter('template_include', 'wpautomate_plugin_templates');
with init action hook
add_action('init','load_custom_template_woo');
function load_custom_template_woo(){
add_filter('template_include', 'wpautomate_plugin_templates');
}
Thanks
For me, I had to set the priory for the hook callback greater than 10, like this
// priority = 11
add_action('template_include', 'wpautomate_plugin_templates', 11);

How to show text in a page on Wordpress whithin a plugin

I am developing a plugin for a website.(Which is my first plugin for Wordpress)
The basic functionality is querying the database and in specific pages show the data from the database with a specific style instead of the content from the pages.
So far I managed to show some text in every specific page.
This is my code after some basic configurations:
global $wpdb;
global $wp_query;
add_action( 'wp', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I've read this post, but I couldn't solve my problem.
WordPress replace content of a page (plugin)
I already read the Wordpress documentation but I havent find anything there.
I found myself a solution, using shortcodes.
global $wpdb;
global $wp_query;
add_shortcode( 'sector_page_display', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function medicalPage()
{
return print "Medical";
}
See that instead of add_action I changed to add_shortcode
Then on everypage I will use to show info from the database I add
[sector_page_display]
in the page, so it call my method. You can add variables in there if you want.
You'll want to run that code before WordPress has fully loaded.
Try this
global $wpdb;
global $wp_query;
add_action( 'init', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I changed the add_action to now run the code when WordPress is being initialized.

Wordpress login_redirect filter gets ignored and has no effect

I'm trying to send users with a specific meta tag to a special page when they log in. I know the $current_user global may not be available, but that the filter passes the user as a parameter, so I think what I'm doing should work:
//special login redirect for employers
function employer_login_redirect($redirect_url, $request_url, $user) {
$user;
$is_employer = get_user_meta($user->ID, 'is_employer', true);
var_dump($is_employer);
if ($is_employer) {
return get_bloginfo('url') . "/candidate-ranks/";
} else {
return $redirect_url;
}
}
add_filter( 'login_redirect', 'employer_login_redirect', 90, 3 ); //late priority just in case other plugins are interfering
Am I missing something? I can't even get the var_dumpt to show up, which makes me think the callback function isn't even getting triggered.
It looks fine but I think you should use site_url() becauseget_bloginfo('url') returns home_url not the site_url. This could be a problrm.
function employer_login_redirect($redirect_url, $request_url, $user) {
$is_employer = get_user_meta($user->ID, 'is_employer', true);
if ($is_employer) {
return site_url() . "/candidate-ranks/";
} else {
return $redirect_url;
}
}
add_filter( 'login_redirect', 'employer_login_redirect', 90, 3 );

Resources