Fetch contact-form-7 id to another plugin

Fetch contact-form-7 id to another plugin
Question :
How to fetch contact-form-7 forms ID, to another plugin?
I want to get forms id and using that id, I will give some effect to that form, please tell me how to fetch form id from contact-form-7, to another plugin in wordpress.
Solution code for : Fetch contact-form-7 id to another plugin
Actually in contact form 7,the post type is wpcf7_contact_form So you can use this bellow code . In tis code the function return an array of all contact form's id.
function get_cf7_IDS(){
   $cf7_id_array =array();
    if ( post_type_exists( 'wpcf7_contact_form' ) ) {
      $args = array(
        'post_type' => 'wpcf7_contact_form',
        'posts_per_page'=>-1,
      );
      $the_query = new WP_Query( $args );
      if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
          $the_query->the_post();
          $cf7_id_array[]= get_the_ID(); 
        }
        wp_reset_postdata();
      }   
  }
  return $cf7_id_array; //RETURN THE ARRAY OF IDS
}
Then use this function to get all ides in array get_cf7_IDS(). Then let me know the result.

How to hook into Contact Form 7 Before Send

Question :
Solution code for : How to hook into Contact Form 7 Before Send

How to hook into Contact Form 7 Before Send

I had to do this to prevent Email from being sent. Hope it helps.
/*
    Prevent the email sending step for specific form
*/
add_action("wpcf7_before_send_mail", "wpcf7_do_something_else");  
function wpcf7_do_something_else($cf7) {
    // get the contact form object
    $wpcf = WPCF7_ContactForm::get_current();

    // if you wanna check the ID of the Form $wpcf->id

    if (/*Perform check here*/) {
        // If you want to skip mailing the data, you can do it...  
        $wpcf->skip_mail = true;    
    }

    return $wpcf;
}

Contact Form 7 AJAX Callback

Question :
Been searching around on this for a while and can't come up with any documentation to outline what i want to achieve.
I'm using wordpress and the Contact Form 7 plugin, all is working perfectly, what i want to achieve is to run some particular javascript upon form submit, I know we can use "on_sent_ok:" in the additional settings, but this only performs if the form is actually submitted.
What I'd like to do is to do some other javascript when the form doesn't submit ok, which throws the user back to the section which didn't validate.
Contact Form 7 AJAX Callback
Solution code for : Contact Form 7 AJAX Callback
Given the variety of responses on this topic the plugin developer seems to change their mind about how this should work every 5 minutes.
document.addEventListener( 'wpcf7mailsent', function( event ) {
  alert( "Fire!" );
}, false );
And the valid events are:
  • wpcf7invalid — Fires when an Ajax form submission has completed successfully, but mail hasn’t been sent because there are fields with invalid input.
  • wpcf7spam — Fires when an Ajax form submission has completed successfully, but mail hasn’t been sent because a possible spam activity has been detected.
  • wpcf7mailsent — Fires when an Ajax form submission has completed successfully, and mail has been sent.
  • wpcf7mailfailed — Fires when an Ajax form submission has completed successfully, but it has failed in sending mail.
  • wpcf7submit — Fires when an Ajax form submission has completed successfully, regardless of other incidents.
Reference: https://contactform7.com/dom-events/

Redirect contact form 7 with javascript

Question :
I do not want to use the additional setting because its filled with a bunch of analytics code. I want to my javascript function to fire when the button is clicked. example the contact form 7's button ID is #aft_submit.
Redirect contact form 7 with javascript
Soution code for : Redirect contact form 7 with javascript
Seen as you are using jquery anyway, you can add the click on the submit button like this
jQuery( document ).ready(function() {
    jQuery("#aft_submit").on('click', function(e){
        e.preventDefault();
        window.location.href  = "http://url-to-redirect.com";
    });
}
You'll need to prevent the default 'submit' action

Wordpress Contact form 7 custom shortcodes

Question :
Contact form 7 has some shortcodes, like [_date] to get todays date. But I want to display the date one week from now.
So I need to create a custom shortcode to Contact form 7 that takes say [next_week] and in the received email the correct date is displayed.
Where and how do I create custom shortcodes to Contact form 7?
Solution code for : Wordpress Contact form 7 custom shortcodes
Adding the following to your functions.php
wpcf7_add_shortcode('custom_date', 'wpcf7_custom_date_shortcode_handler', true);

function wpcf7_custom_date_shortcode_handler($tag) {
    if (!is_array($tag)) return '';

    $name = $tag['name'];
    if (empty($name)) return '';

    $next_week = date('Y-m-d', time() + (60*60*24*7)); 
    $html = '';
    return $html;
}
Now in the "Form" field in CF7 GUI type [custom_date next_week]
Now you can use [next_week] in the message body.

Write HTML in PHP

Examples for : Write HTML in PHP

With print command , you can output HTML code inside php.

Sample code :

print("This is point to print.");

To use quotes inside print command , use \" inside code.

print("Visit Google");

With echo , html output can be done inside php.

echo "

This is HTML code inside echo syntax inside php

";

WooCommerce: Easily Get Product Info (ID, SKU, $) from $product Object

I have been wanting to publish this guide for a long while. As a freelancer, every day I repeat many operations that make me waste time – and one of them is indeed "How to get ___ if we have the $product variable/object?".
For example, "How can we get the product SKU?? Or "How can we get the product short description"? Or maybe the product stock level, shipping class, tax class, price, regular price, sale price, and so on.. Hopefully this article will save you time
Get Product Info

1. If we have access to $product

// Get Product ID
 
$product->get_id(); (fixes the error: "Notice: id was called incorrectly. Product properties should not be accessed directly")
 
// Get Product General Info
 
$product->get_type();
$product->get_name();
$product->get_slug();
$product->get_date_created();
$product->get_date_modified();
$product->get_status();
$product->get_featured();
$product->get_catalog_visibility();
$product->get_description();
$product->get_short_description();
$product->get_sku();
$product->get_menu_order();
$product->get_virtual();
get_permalink( $product->get_id() );
 
// Get Product Prices
 
$product->get_price();
$product->get_regular_price();
$product->get_sale_price();
$product->get_date_on_sale_from();
$product->get_date_on_sale_to();
$product->get_total_sales();
 
// Get Product Tax, Shipping & Stock
 
$product->get_tax_status();
$product->get_tax_class();
$product->get_manage_stock();
$product->get_stock_quantity();
$product->get_stock_status();
$product->get_backorders();
$product->get_sold_individually();
$product->get_purchase_note();
$product->get_shipping_class_id();
 
// Get Product Dimensions
 
$product->get_weight();
$product->get_length();
$product->get_width();
$product->get_height();
$product->get_dimensions();
 
// Get Linked Products
 
$product->get_upsell_ids();
$product->get_cross_sell_ids();
$product->get_parent_id();
 
// Get Product Variations
 
$product->get_attributes();
$product->get_default_attributes();
 
// Get Product Taxonomies
 
$product->get_categories();
$product->get_category_ids();
$product->get_tag_ids();
 
// Get Product Downloads
 
$product->get_downloads();
$product->get_download_expiry();
$product->get_downloadable();
$product->get_download_limit();
 
// Get Product Images
 
$product->get_image_id();
get_the_post_thumbnail_url( $product->get_id(), 'full' );
$product->get_gallery_image_ids();
 
// Get Product Reviews
 
$product->get_reviews_allowed();
$product->get_rating_counts();
$product->get_average_rating();
$product->get_review_count();
 
// source: https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html

2. If we have access to $product_id

If we have access to the product ID (once again, usually the do_action or apply_filters will make this possible to we), we have to get the product object first. Then, do the exact same things as above.
// Get $product object from product ID
 
$product = wc_get_product( $product_id );
 
// Now you have access to (see above)...
 
$product->get_type();
$product->get_name();
// etc.
// etc.

3. if we have access to the Order object or Order ID

How to get the product information inside the Order? In this case you will need to loop through all the items present in the order, and then apply the rules above.
// Get $product object from $order / $order_id
 
$order = new WC_Order( $order_id );
$items = $order->get_items();
 
foreach ( $items as $item ) {
 
    $product = wc_get_product( $item['product_id'] );
 
    // Now you have access to (see above)...
 
    $product->get_type();
    $product->get_name();
    // etc.
    // etc.
 
}

4. You have access to the Cart object

How to get the product information inside the Cart? In this case, once again, you will need to loop through all the items present in the cart, and then apply the rules above.
// Get $product object from Cart object
 
$cart = WC()->cart->get_cart();
 
foreach( $cart as $cart_item ){
 
    $product = wc_get_product( $cart_item['product_id'] );
 
    // Now you have access to (see above)...
 
    $product->get_type();
    $product->get_name();
    // etc.
    // etc.
 
}

Show original image rather than thumbnail for product on shop page in woocommerce

Solution for : Show original image rather than thumbnail for product on shop page in woocommerce

//show full image on wocommerece list items
remove_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
add_action('woocommerce_before_shop_loop_item_title', 'new_woocommerce_template_loop_product_thumbnail', 10);

function new_woocommerce_template_loop_product_thumbnail() {
 $imgurl= wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
 echo '';
}

Woocommerce get category image full size

Solution for : Woocommerce get category image full size
We can use this wp_get_attachment_image_src function to achieve the goal.
$prod_categories = get_terms( 'product_cat', array(
        'orderby'    => 'name',
        'order'      => 'ASC',
        'hide_empty' => true
    ));

    foreach( $prod_categories as $prod_cat ) :
        $cat_thumb_id = get_woocommerce_term_meta( $prod_cat->term_id, 'thumbnail_id', true );
        $shop_catalog_img = wp_get_attachment_image_src( $cat_thumb_id, 'full' );
        $term_link = get_term_link( $prod_cat, 'product_cat' );?>

         echo '
'; echo $prod_cat->name; echo '----'; echo ''; echo '
'; endforeach; wp_reset_query();

Exclude multiple terms using get_terms() function

Soution for : exclude multiple terms using get_terms() function

With get_terms(), the exclude parameter takes an array of term IDs, so just add the second term to the array:

$terms = get_terms( 'product_cat', array(
    'orderby'    => 'name',
    'order'      => 'ASC',
    'hide_empty' => 0,
    'exclude' => array( 23 ),
    ));

echo '
  • Category:
  • '; foreach ( $terms as $term ) { echo '
  • '.$term->name.'
  • '; }

    WooCommerce Notice Messages, how do I edit them?

    I'm trying to figure out where WooCommerce creates it's messages for when there is a success, error or notice in WooCommerce. I want to edit those messages to fit the scenario more neatly and also edit the HTML. Where are these messages located and how do I edit them?

    Solution code for : WooCommerce Notice Messages, how do I edit them?

    Many of them are directly in the plugin files - unfortunately. Some messages are tied to filter hooks that allow you to edit them without messing with plugin files but that's not always the case.

    The message you wanted to change was "Product Name was successfully added to your cart". This one is set in the function wc_add_to_cart_message in wc-cart-functions.php and this function allows you to change it using a filter:

    wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
    

    So in your functions.php file you could add something like:

    add_filter('wc_add_to_cart_message', 'handler_function_name', 10, 2);
    function handler_function_name($message, $product_id) {
        return "Thank you for adding product" . $product_id;
    }
    

    Setting variable in header.php but not seen in footer.php

    In wordpress , I set a variable in header.php
    $var= 'anything';
    
    but in footer.php when I echo it
    echo $var;
    
    I got no thing printed. why?
    Solution for : setting variable in header.php but not seen in footer.php

    You're not in the same scope, as the header and footer files are included in a function's body. So you are declaring a local variable, and referring to another local variable (from another function).

    So just declare your variable as global:
    $GLOBALS[ 'var' ] = '...';
    
    Then you can echo it globally.
    echo $GLOBALS[ 'var' ];
    

    How To Create Custom User Role in Wordpress

    Soution code for : How To Create Custom User Role in Wordpress

    You can use add role function like

    add_role( $role, $display_name, $capabilities );
    

    Example

    add_role('basic_contributor', 'Basic Contributor', array(
        'read' => true, // True allows that capability
        'edit_posts' => true,
        'delete_posts' => false, // Use false to explicitly deny
    ));
    

    Add a custom class name to Wordpress body tag

    Soution code for : Add a custom class name to Wordpress body tag

    We can use the body_class filter, like so:

    function my_plugin_body_class($classes) {
        $classes[] = 'foo';
        return $classes;
    }
    
    add_filter('body_class', 'my_plugin_body_class');
    

    Although, obviously, our theme needs to call the corresponding body_class function.

    How to horizontally center a <div> using CSS?

    How can we horizontally center a <div> within another <div> using CSS?

    Foo foo

    Solution code for : How to horizontally center a <div> using CSS?

    We can apply this CSS to the inner <div>:

    HTML

    Foo foo

    CSS

    #inner {
      width: 50%;
      margin: 0 auto;
    }
    

    Of course, we don't have to set the width to 50%. Any width less than the containing <div> will work. The margin: 0 auto is what does the actual centering.

    If we are targeting IE8+, it might be better to have this instead:

    HTML

    Foo foo

    CSS

    #inner {
      display: table;
      margin: 0 auto;
    }
    

    It will make the inner element center horizontally and it works without setting a specific width.