Wordpress: map existing page content instead of a copy - wordpress

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/

Related

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

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.

Custom Page Title Shortcode within another Shortcode

I use this function to obtain the title of post page in the content area:
function myshortcode_title( ){
return get_the_title();
}
add_shortcode( 'page_title', 'myshortcode_title' );
So, I have a plugin that when I use this shortcode below it read the text beetween the shortcode:
[responsivevoice voice="UK English Male"] Wonderful World [/responsivevoice]
So, what I'm doing and it's my question also is how can I put the "page title shortcode" within responsivevoice shortcode like this?
[responsivevoice voice="UK English Male"] [page_title] [/responsivevoice]
(It should get the title of the post page, but it doesn't work).
Choose one of the following options, and add the code snippet after:
add_shortcode( 'page_title', 'myshortcode_title' );
Option #1: [responsivevoice2]
add_shortcode( 'responsivevoice2', 'responsivevoice2' );
function responsivevoice2( $atts = array(), $content = '' ) {
if ( $content = do_shortcode( $content ) ) {
return RV_add_bblisten( $atts, $content );
}
return '';
}
Sample usage:
[responsivevoice2 voice="UK English Male"] [page_title] [/responsivevoice2]
This Shortcode allows you to use the [page_title] or any other Shortcodes in the
text to be spoken.
Option #2: [responsivevoice_page_title]
This Shortcode will always use the page title (or the title of the current post)
as the text to be spoken. With this Shortcode, you don't need the [page_title]
Shortcode anymore.
add_shortcode( 'responsivevoice_page_title', 'responsivevoice_page_title' );
function responsivevoice_page_title( $atts = array() ) {
if ( $page_title = get_the_title() ) {
return RV_add_bblisten( $atts, $page_title );
}
return '';
}
Sample usage:
[responsivevoice_page_title voice="UK English Male" /]

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.

Only show images/attachments within the gallery?

When i create a page, add a gallery, and browse this gallery on the front-end it will browse all the attachments that are associated with that page, instead of just the images within that gallery. Is there a way to filter all the other attachments and only show the images within a certain gallery? So that, for instance, when I delete the gallery and add a new gallery on the same page > only the new gallery is shown?
Any ideas?
This might not be the most elegant way, but i've found it very usefull.
Passing a post ID to the function below will load a gallery from the post_content of that post. So you would create a gallery and insert it into your post content, then in the template you run this function and will be returned with an array of attachments in that gallery which you are free to to whatever with, i.e slideshows and the likes.
function wp_load_gallery($post_id) {
$post = get_post( $post_id );
$regx = '/' . get_shortcode_regex() . '/';
preg_match( $regx, $post->post_content, $matches );
$ids = shortcode_parse_atts( $matches[3] );
$gallery = array( );
foreach( explode( ',', $ids['ids'] ) as $id ) {
if($id) {
$gallery[] = get_post( $id );
}
}
return $gallery;
}
Note that the shortcode is not cut from the content, so when you display the content you should run it through the strip_shortcodes function, i.e:
echo strip_shortcodes( get_the_content() );
This allows you to update the gallery whenever you want with whatever you want.
EDIT:
To simply display all images:
$gallery = wp_load_gallery($YOUR_POST_ID);
foreach($gallery as $image) {
echo wp_get_attachment_image($image->ID);
}

How to insert a post into a page in wordpress? Using [Shortcodes]

I am using wordpress as our CMS for our companies website.
We have about 5-10 pages which we want to insert the same call to action content in.
How can I create a shortcode that will allow me to embed the content from a post into a page?
Thanks
Steven.
I haven't tested this, but you'll probably want something along these lines:
function create_call_to_action_shortcode( $atts ) {
$call_to_action_content = '';
$query = new WP_Query( array( 'p' => $post_id, 'no_found_rows' => true ) );
if( $query->have_posts() ) {
$query->the_post();
remove_filter( 'the_content', 'sharing_display', 19);
$call_to_action_content = apply_filters( 'the_content', get_the_content() );
add_filter( 'the_content', 'sharing_display', 19);
wp_reset_postdata();
}
return $call_to_action_content;
}
add_shortcode( 'call_to_action', 'create_call_to_action_shortcode' );`
In your pages ( or other posts, for that matter ), you can simply insert [call_to_action] in the page/post content.
You can find more information on shorcodes here. :)
Edit:
In order to remove sharing buttons from the post content, you need to call remove_filter( 'the_content', 'sharing_display', 19);. I've update the code above.

Resources