WordPress apply_filters( 'the_content', 'func_name' ) not working inside another hook - wordpress

I am trying to display PAGE Content inside Product Single Page based on some conditions met.
I am showing this Page Content at a particular action hook place of Product Single Page
as shown in below code :
add_filter( 'wp', array( $this, 'txb_add_page_check_single' ) );
public function txb_add_page_check_single() {
if ( is_single() ) {
if ( some_condition_met ) {
add_action( 'woocommerce_after_add_to_cart_quantity', array( $this, 'txb_display_page_content_single' ) );
}
}
}
public function txb_display_page_content_single() {
$txb_all_data_array = get_post( $some_page_id );
//echo apply_filters( 'the_content', get_post_field( 'post_content', $txb_all_data_array->ID ) ); // CASE 1 : NOT WORKING
$get_content_data = $txb_all_data_array->post_content;
echo $get_content_data; //CASE 2 : Working
}
Here // CASE 1 is not working.
// CASE 1 is returning a CURRENT SINGLE PRODUCT CONTENT, but I want PAGE CONTENT of $some_page_id.
I want to get it work with // CASE 1 , Because Content may have Shortcode or any other Gutenberg block.
PS : hook name woocommerce_after_add_to_cart_quantity will come dynamically , based on some conditions met.

Place this code in your active theme's functions.php file
OR Custom Plugin's main file
add_filter('se152488_the_content', 'wptexturize');
add_filter('se152488_the_content', 'convert_smilies');
add_filter('se152488_the_content', 'convert_chars');
add_filter('se152488_the_content', 'wpautop');
add_filter('se152488_the_content', 'shortcode_unautop');
add_filter('se152488_the_content', 'prepend_attachment');
then , call content like this :
echo apply_filters( 'se152488_the_content' , $txb_all_data_array->post_content );
Credit : https://wordpress.stackexchange.com/a/152493/110795

The variable $some_page_id needs to be set, its passing null and you're getting the CURRENT SINGLE PRODUCT CONTENT.

Related

How to add another Add To Cart and product variant selection in Woocommerce

How can I add another add to cart button as well as product variant selection above product description in my Woocommerce product page?
This will add another variable product selector above the short description. For simple products they will remain unchanged. This code was taken from the function woocommerce_variable_add_to_cart which I have basically copied and just removed the wp_enqueue_script call as this is already included in the usual variation selector already present. Paste this code into your theme's functions.php file.
function add_product_variation_selector() {
global $product;
// Enqueue variation scripts.
//wp_enqueue_script( 'wc-add-to-cart-variation' );
if( $product->is_type( 'variable' ) ) :
// Get Available variations?
$get_variations = count( $product->get_children() ) <= apply_filters( 'woocommerce_ajax_variation_threshold', 30, $product );
// Load the template.
wc_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $get_variations ? $product->get_available_variations() : false,
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_default_attributes(),
) );
endif;
}
add_action( 'woocommerce_single_product_summary', 'add_product_variation_selector', 9 );
Clone the plugins/woocommerce/templates/content-single-product.php to your theme following the instructions from the file.
Change it according to your needs.

Where would I add this add_action() code influencing the product admin view?

I have a piece of code from another answer that customizes the way products are displayed in the admin view. It appears to be adding a WordPress hook using add_action().
add_action( 'manage_product_posts_custom_column', 'wpso23858236_product_column_offercode', 10, 2 );
function wpso23858236_product_column_offercode( $column, $postid ) {
if ( $column == 'offercode' ) {
echo get_post_meta( $postid, 'offercode', true );
}
}
However, I am unclear where I have to place this code for it to have the desired effect.

Add second single product page in Woocommerce

Is it possible to add a second single product page in Woocommerce?
So basically when I am at the single product page I click the "next" button and I get directed to the same single product page with another template. So I just want to retrieve the same data on the second page.
Single product page:
Next page:
And the next page would be the checkout page but that would just be a link to the checkout page so that part would be easy.
What I would do in this case is add a link that will reload the page with a custom query arg in the URL.
Then you can filter the template via template_include to load a different template. Untested, so be careful of syntax typos.
add_filter( 'template_include', 'so_30978278_single_product_alt' );
function so_30978278_single_product_alt( $template ){
if ( is_single() && get_post_type() == 'product' && isset( $_GET['next-step'] ) && intval( $_GET['next-step'] ) == 1 ) {
$template = locate_template( 'single-product-alt.php' );
}
return $template;
}
add_action( 'woocommerce_before_add_to_cart_form', 'so_30978278_additional_template_button' );
function so_30978278_additional_template_button(){
printf( '<a class="button" href="%s">%s</a>', esc_url( add_query_arg( 'next-step', 1 ) ), __( 'Next Step' ) );
}
See add_query_arg for reference.

WordPress How to Keep the_content filter for my cpt from affecting other posts on the same page

I am using a custom post type for the slide in my theme. I'm trying to remove wpautop from the cpt using the_content filter hook, and it works using the following code, but it also removes it wpautop from the other queries on the same page. Here is the code:
add_filter( 'the_content', 'remove_autop_for_post_type', 0 );
function remove_autop_for_post_type( $content )
{
if('par2_slide' === get_post_type(get_the_ID())){
remove_filter( 'the_content', 'wpautop' );
return $content;
};
return $content;
};
Adding a second condition to include "! is_main_query() like so:
if('par2_slide' === get_post_type(get_the_ID()) && ! is_main_query()){
causes the script to stop working. The query for my cpt to which this is supposed to apply is as follows:
function par2_slides_query () {
$args = array( 'post_type' => 'par2_slide');
$slides_loop = new WP_Query( $args );
while ( $slides_loop->have_posts() ) : $slides_loop->the_post();
echo '<li class="slide">';
the_content();
echo '</li>';
endwhile;
};
wpauto messes with the slide layout so I really need to filter the content for that particular post type to turn it off without turning it off for the instances of the_content on the rest of the page. It doesn't affect other pages, on the_content on the same page.
Re add the filter after your IF statement.
add_filter('the_content', 'wpautoop');
It removes it and returns the content IF par2_slide is the post type, and if it's not it adds the filter if it doesn't exist.

Wordpress: map existing page content instead of a copy

I have a page with several subpages in wordpress. The page should display the same content as the second subpage. When I edit the page, there is an option to copy the content from the subpage into it, but then I would have to maintain two instances of the text. Is there a possibility to map existing page content into a page? I'm using Wordpress.com so I cannot edit any files/links on the server.
You could use a shortcode that fetches the content of a page specified. In functions.php:
add_action( 'init', 'so20477735_init', 11 );
function so20477735_init()
{
add_shortcode( 'duplicate_page', 'so20477735_shortcode_callback' );
}
function so20477735_shortcode_callback( $atts, $content = null )
{
extract( shortcode_atts( array(
'page_id' => 0
), $atts ) );
$page_data = get_page( $page_id );
$return = '';
if( ! is_null( $page_data ) )
$return .= $page_data->post_content;
return $return;
}
Example usage:
[duplicate_page page_id="12"]
EDIT
I missed the fact you're using wp.com. You should be able to do something similar with the display posts shortcode:
http://en.support.wordpress.com/display-posts-shortcode/

Resources