GOAL: Use the wpcf7_form_response_output filter to dynamically alter the "Success message" that is shown to user upon successful form submission.
ISSUE: When wp_footer is called in footer.php, the filter I have in place will not work - ever. However, when wp_footer is disabled/removed, the filter will work. Additionally, I noticed that when wp_footer is not active, after submission CF7 wil add the form ID to the URL (i.e., #wpcf7-f2294-p2295-o1). Unfortunately I need wp_footer enabled.
MY FILTER CODE:
function filter_wpcf7_response_output( $output ){
$date = date('Ymd');
if( strpos($output, 'wpcf7-mail-sent-ok') ) {
var_dump($output);
echo "in if statement";
?>
<script>
jQuery(function ($) {
$(document).ready(function () {
var newOutput = "Reference ID:" + <?=$date?> + "-00" + "5"+ ". Thank you for your message. It has been sent.";
$('.wpcf7-mail-sent-ok').html(newOutput);
});
});
</script>
<? }
return $output;
}
add_filter( 'wpcf7_form_response_output', 'filter_wpcf7_response_output', 10, 1);
ATTEMPTS:
Tried keying off of event listeners for "mailing send" event.
Tried "hardcoding" solution via .on("click", ...) jQuery method. Ultimately, could not alter html of "success message" because said message is not rendered to DOM, so jQuery cannot locate it.
ADDITIONAL INFO:
Theme utilized is a custom theme using Twenty Fifteen as model/core
Cannot share site-specific information as this is for business client
POSSIBLE RESOLUTION:
One way I was able to have wp_footer called in footer.php and utilize the above filter is if I disabled CF7 script, using: add_filter( 'wpcf7_load_js', '__return_false' );.
Related
I'm training on the creation / modification of wordpress plugin, to do this, I need to make a simple PHP script that performs different actions. The problem is that once the do_action is called, nothing happens.
<?php
[...]
$actions_to_do = [
'wp_ajax_lorem_ipsum'
];
foreach ($actions_to_do as $action) {
$output = do_action($action);
echo $output;
}
I've seen :
Are there any drawback of using output buffer on do_action function?
WordPress: save output of do_action in variable
And tried this trick with the ob_start() but the problem remains the same.
foreach ($actions_to_do as $action) {
ob_start();
do_action($action);
$output = ob_get_contents();
ob_end_clean();
echo $output;
}
I specify that in the state I do not test with a plugin in particular because the problem is the same whatever the plugin or the action
Think of do_action() as a placeholder for a possible action. Here is simple example to illustrate it:
<?php
// You can paste this in page.php, in header of footer, or any other place that would be easy to observe (for test purpose only!)
echo "Hello";
do_action( 'hello_name' );
echo "!";
?>
By simply placing this code and opening a corresponding page you will be presented with:
Hello!
While do_action() was executed, there were no actions associated with it.
Now without removing existing code, add the following to your functions.php file:
<?php
/**
* We register a custom action which will be processed by
* WordPress when do_action( 'hello_name' ) is executed.
*/
function my_name_is() {
echo "Tom";
}
add_action( 'hello_name', 'my_name_is' );
?>
Reloading the page should now display:
Hello Tom!
You may register multiple actions to be triggered when do_action( 'hello_name' ) function is executed using add_action() function.
So I need to hide basically everything on the checkout page, but of course post everything to the backend. I dont want to unset as this removes the field and on the My Acount Billing address we've added alot of custom fields so the address is intact there. But on the checkout process I dont want to show the fields again (theyve already been set to readonly when on checkout) , is it possible using jQuery that only if on checkout to hide it from the user but still everything on the backend works as intended ?
The CSS Display hidden code works for majority but for three it just wont for some reason:
1 - Street address
House number and street name
The label and the field
Town / City
Just the labels
Postcode / ZIP
Just the labels
Codeadd_action( 'wp_footer', 'custom_hide_country_field' );
function custom_hide_country_field() {
if ( is_checkout()) {
echo "<script type='text/javascript'>
jQuery('[id=\"billing_country_field\"]').css('display','none');
jQuery('[id=\"billing_title\"]').css('display','none');
jQuery('[id=\"billing_title_field\"]').css('display','none');
jQuery('[id=\"billing_condition_field\"]').css('display','none' );
jQuery('[id=\"billing_address_1_field\"]').css('display','none, !important;');
jQuery('[id=\"billing_suburb_field\"]').css('display','none');
jQuery('[id=\"billing_suburb\"]').css('display','none');
jQuery('[id=\"billing_city\"]').css('display','none');
jQuery('[id=\"billing_postcode\"]').css('display','none');
jQuery('[id=\"billing_complex_address_inside_field\"]').css('display','none');
jQuery('[id=\"billing_complex_address_inside\"]').css('display','none');
jQuery('[id=\"billing_complex_name_field\"]').css('display','none');
jQuery('[id=\"billing_complex_name\"]').css('display','none');
jQuery('[id=\"billing_complex_other_field\"]').css('display','none');
jQuery('[id=\"billing_complex_other\"]').css('display','none');
</script>";
}
}
If I add it in console it works but from the script only those 3 just won’t work for some reason
Please try something like this:
//hide billing country field in checkout page
add_action( 'wp_footer', 'custom_hide_country_field' );
function custom_hide_country_field() {
if ( is_checkout()) {
echo "<script type='text/javascript'>
$('[id=\"billing_country_field\"]').css('display','none');
</script>";
}
}
CSS targeted it using the following:
#customer_details .woocommerce-billing-fields__field-wrapper .form-row, #customer_details .woocommerce-additional-fields__field-wrapper .form-row {
display: none !important;
}
I am trying to set custom message on subject textbox after form load. So that I can set the custom values like customerID=1 and then wanted to place some if conditions by checking if cutomerID == 1 then add subject line "Hello john" and if customerID==2 then add subject line "hello james" on the form textbox
like here:
So in my wordpress site I wanted to add hook to customise subject line based on the condition but dont know which hook is appropriate to do this?
add_action( 'wpcf7_form_autocomplete', 'my_change_subject' );
function my_change_subject( $wf7 ) {
if($_GET['customerid'] == 1) {
<script>$("input[name='subject']").val('hello john');</script>
}
} ?>
Something like above but what hook I should use to do this?
I am also adding hooks site reference here: contact form7 hook reference site
Not sure if anyone comes to this issue before but I found simple solution;)
add_action( 'wpcf7_form_autocomplete', 'my_change_subject' );
function my_change_subject( $wf7 ) {
?>
<script>
jQuery(document).ready(function(){
//console.log('coming here...<?php echo $_GET['cid'];?>');
document.getElementsByName('your-subject')[0].value = "Customer Enquiry: <?php echo $_GET['cid'];?>";
});
</script>
<?php
}
} ?>
I'm creating a custom woocommerce integrated theme for wordpress.
I have a blob on the top that displays the total number of items in the cart, I want to update this blob using Jquery (w/o reloading the page) I was able to increase the number of items by getting the current number in the blob and increasing it by +1 for each click, the problem is the add to cart has an option to select the number of items you want to add to the cart. So if I select 3 items and click the button the blob only increases by one.
I can create a way to get the number of items being added from the front-end but I think it's unnecessary. I want to be able to get the total number from PHP sessions using jquery so that on every click of add item or remove item I'll get the current number dynamically from the server.
What I have done so far is to create a reloadCart.php file that echos the cart total, here's the code
<?php
require('../../../wp-blog-header.php');
global $woocommerce;
echo $woocommerce->cart->get_cart_contents_count();
?>
When I visit this page it echos the current item totals, but I cant get this data from jquery, it's been sometime since I last used AJAX also I have not worked on web projects for a very long time, but with what I remember, the AJAX call that I'm making is right.
I have tried using the get() and post() functions of jquery as well as the normal ajax() function, but nothing seems to work. Can someone please help?
$(".ajax_add_to_cart").click(function () {
/*$("#bag-total").html(function () {
var bagTotal = parseInt($(this).html());
return ++bagTotal;
});*/
alert('clicked');
$.get("<?php echo get_template_directory_uri(); ?>/reloadCart.php", function(data){
alert("Data: " + data);
});
});
The lines that are commented are the ones that I was using previously, to add the cart total by getting the current cart number from the front-end.
Any help would be appreciated. Thanks in advance!
You should not use any reload to update the cart content count… Instead you should use the dedicated woocommerce_add_to_cart_fragments action hook that is Ajax powered.
1) The HTML to be refreshed: So first in your theme's header.php file you should need to embed the cart count in a specific html tag with a defined unique ID (or a class), for example something like:
$items_count = WC()->cart->get_cart_contents_count();
?>
<div id="mini-cart-count"><?php echo $items_count ? $items_count : ' '; ?></div>
<?php
or:
$items_count = WC()->cart->get_cart_contents_count();
echo '<div id="mini-cart-count"><?php echo $items_count ? $items_count : ' '; ?></div>';
2) The code:
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_mini_cart_count');
function wc_refresh_mini_cart_count($fragments){
ob_start();
$items_count = WC()->cart->get_cart_contents_count();
?>
<div id="mini-cart-count"><?php echo $items_count ? $items_count : ' '; ?></div>
<?php
$fragments['#mini-cart-count'] = ob_get_clean();
return $fragments;
}
if you use a class in your html Tag, you will replace ['#mini-cart-count'] by ['.mini-cart-count']. This hook is also used to refresh the mini-cart content.
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Since few years global $woocommerce; + $woocommerce->cart is outdated and replaced by WC()->cart to access WooCommerce cart object.
If you need jQuery to force refresh that count, you can try wc_fragment_refresh or wc_fragments_refreshed delegated events, like:
$(document.body).trigger('wc_fragment_refresh');
or:
$(document.body).trigger('wc_fragments_refreshed');
For anyone who wants the proper ajax implementation, here is the way to go.
in functions.php
add_action('wp_ajax_cart_count_retriever', 'cart_count_retriever');
add_action('wp_ajax_nopriv_cart_count_retriever', 'cart_count_retriever');
function cart_count_retriever() {
global $wpdb;
echo WC()->cart->get_cart_contents_count();
wp_die();
}
in your script file (assuming you have enqued the script file and passed the ajax object into the script. you also need to put this block into a setInterval or in some other jquery action.
var data = {
'action': 'cart_count_retriever'
};
jQuery.post(ajax_object.ajax_url, data, function(response) {
alert('Got this from the server: ' + response);
});
In header.php or where you want to show count
<?php $items_count = WC()->cart->get_cart_contents_count();
echo $items_count; //use this function for print the value of cart items count
?>
I have not used woocommerce before but one pretty simple option when you say in your post:
When I visit this page it echos the current item totals, but I cant get this data from JQuery
...would be to use a user-sided JavaScript variable for the display, and then just call the PHP update methods for adding items to your cart using AJAX (which I do not show below because you have not provided that code).
<?php
//hardcoded value for $woocommerce->cart->get_cart_contents_count()
$woocommerce = 59;
?>
<button class="ajax_add_to_cart">Add to cart</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
//user sided variable for PHP value
var total = parseInt($(".totalCost").text());
$(".ajax_add_to_cart").click(function(){
total++; //add to cart
$(".totalCost").text(total); //update
});
});
</script>
<p class="totalCost">
<?php echo json_encode($woocommerce); ?>
</p>
You can copy and test this snippet on: http://phpfiddle.org/
Basically in the above code, I set the PHP value as a paragraph text on page load and then read that value into a JS variable to mess around with the data on the client side of the application and then I update the display text as needed.
function woocommerce_header_add_to_cart_fragment( $fragments ) {
$fragments['li.cart-open'] = '<li class="cart-open"><a href="javascript:void(0)" class="cart" title="Cart">
<svg xmlns="http://www.w3.org/2000/svg" width="96" height="96" viewBox="0 0 96 96" class="svg-large"><switch><g><path d="M68 24v-4C68 8.954 59.046 0 48 0S28 8.954 28 20v4H12v60c0 6.63 5.37 12 12 12h48c6.63 0 12-5.37 12-12V24H68zm-32-4c0-6.627 5.373-12 12-12s12 5.373 12 12v4H36v-4zm40 64c0 2.21-1.79 4-4 4H24c-2.21 0-4-1.79-4-4V32h56v52z"/></g></switch></svg>
<i>'.WC()->cart->get_cart_contents_count().'</i>
</a></li>';
return $fragments;
}
First, I'd like to pre-emptively apologize for doing anything naive or foolish. I'm a novice programmer, but I'm trying to learn. Right now, I'm trying to develop a plugin for wordpress as part of my internship. The plugin does what it needs to do for the most part, but I just can't seem to make the settings page work the way I want. I know the initialization part of the plugin is working properly, but I'm having trouble with using the settings page. Specifically, I don't where or how to use _POST in order to save the information. I also don't where to place the update_option function call after I've received the information from the settings page. Here's my code right now:
add_action( 'admin_menu', 'menu' );
add_action( 'admin_init', 'plugin_admin_init' );
function plugin_admin_init() { // whitelist options
register_setting( 'plugin_options', 'plugin_options', 'plugin_options_validate' );
add_settings_section('plugin_main', 'Main Settings', 'plugin_section_text', 'plugin');
add_settings_field('plugin_text_string', 'REE View Count Settings', 'plugin_setting_string', 'plugin', 'plugin_main');
}
function menu() {
add_options_page( 'REE View Count Options', 'REE View Count', 'manage_options', 'plugin', 'plugin_options_page' );
}
// generates the settings web page
function plugin_options_page() {
echo '<div class="wrap">';
echo '<h2>Options</h2>';
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
// In the line above, I don't know what to put. The file name of the plugin?
// The URL of the settings page on the admin page? Or what I have currently?
// Both the first and last options bring up a 404 page.
settings_fields('plugin_options');
do_settings_sections('plugin');
echo '<input name="Submit" type="submit" value="' . esc_attr_e('Save Changes') . '" />';
echo '</form>';
//update_option('plugin_options', $_POST['plugin_options']);
// Where should I try to update the option? And how?
echo '</div>';
}
//from add_settings_section parameter 3, in plugin_admin_init
function plugin_section_text() {
echo '<p>Main description of this section here.</p>';
}
//Function from 3 paramter of add_settings_field in plugin_admin_init that outputs input HTML
function plugin_setting_string() {
$options = get_option('plugin_options');
echo 'value of options is: ' . $options;
echo "<input id='plugin_text_string' name='plugin_options' size='40' type='text'
value='{$options}' />";
}
//Validation function from register_setting in plugin_admin_init
function plugin_options_validate($input) {
echo "this is input: " . $input;
$newinput = trim($input);
if(!preg_match('/^[0-9]+$/', $newinput)) {
$newinput = 10;
}
update_option('plugin_options', $newinput);
return $newinput;
}
Note: I don't actually use these exact function/variable names - I have them prefixed with the actual plugin name.
Can you help me? I want to take the information the admin inputs into the settings page and update the database with the that information (which in this case is just a single number). I understand that I need to use PHP _POST, but I don't know how. Furthermore, I don't where to post to, because when I use action="the_file_name.php," I get a 404 error upon submission. What should the action of the form be so that I can use the information that I got from the admin submission and use it for later? And after I do that, how do update the setting? And by how, I mean where do I place update_option? I apologize if this seems rambly or vague - I feel like I'm somewhat over my head.
If it helps, I've been building this settings page in the same file as the plugin itself, with the help of this guide: http://ottopress.com/2009/wordpress-settings-api-tutorial/
Unfortunately, I don't see anything in that guide that speaks of updating information, just creating the page itself.
You want to point to action="options.php". It handles updating the values. Check out the following links to get more detailed info.
http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
http://www.sitepoint.com/wordpress-options-panel/