I have Woocommerce (Subscriptions) and Elementor. I'm trying to add a page/content within the Myaccount area of Woocommerce - new navigation menu item.
Creating the endpoint and navigation menu bit works without issue. The issue I'm facing is displaying a page created in elementor. One page (also created in elementor) works without issue while the other doesn't.
The page created in elementor is fairly simple that essentially creates 4 columns, 10 rows. Within each row there is button that uses shortcodes to get the button text and url to navigate to when pressed. This is all tested and works without issue when accessing the page directly.
If I use this code
$post = get_post(1114);
$content = apply_filters('the_content', $post->post_content);
echo $content;
on the endpoint to display the page the output is just a list of rows of text showing the table cells from left to right. This only shows the button text (no sign of the URL) and is not formatted in anyway like the page in the elementor editor is (or if accessed directly)
e.g if the table is
H1 H2 H3 H4
R1a R1b R1c R1d
R2a R2b R2d R2d
The display is
H1
H2
H3
R1a
R1b
R1c
R1d
R2a
R2b
R2c
R2d
If I use the below code
$content = \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( 1119);
echo $content;
the table largely displays correctly with all formatting etc. The one thing that isn't working is the button text. Instead of displaying the text returned by shortcode it just displays the shortcode.
I'm sure I'm just missing something that needs to be processed somewhere but I have no idea what it is and the Elementor pages don't give much away unfortunately.
Any help would be appreciated.
EDIT : The correct answer is that there is a dynamic button/dropdown next to the text field that I didn't notice before. Using this you can select shortcode and enter the shortcode details and it will display correctly without having to process manually.
Not sure why it doesn't load correctly as above in the first place but to get around it I did the below. No sure this is the correct way to do this but it works.
$content = \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( 1013 );
$content = process_subswitch_content_for_shortcodes( $content );
echo $content;
The below essentially searches the content returned from the get_builder_content_for_display function for '[subswitch' which is the start of the shortcodes in question (unable to just search for ] as elementor puts other [] in the content).
function process_subswitch_content_for_shortcodes( $content ) {
$keepprocessing = true;
$searchstart_pos = 0;
do {
$startchar_pos = strpos( $content, "[subswitch", $searchstart_pos );
if ( $startchar_pos == false ) {
$keepprocessing = false;
}
$endchar_pos = strpos( $content, "]", $startchar_pos );
$shortcode_request = substr( $content, $startchar_pos, $endchar_pos );
if ( $shortcode_request == false ) {
$keepprocessing = false;
}
$shortcode_content = do_shortcode( $shortcode_request );
$content = str_replace( $shortcode_request, $shortcode_content, $content );
$searchstart_pos = $endchar_pos;
} while ( $keepprocessing );
return $content;
}
I'd of course still like to know the way to load this directly so that it displays without having to process the shortcodes manually so to speak.
Related
I have a WP shortcode that is giving me problems.
Basically, the shortcode just pulls content from another post using a couple of parameters. It then loads up a partial template.
The problem occurs in WP Admin when saving the page that contains the shortcode. When saving the page updates do in fact save correctly but the resulting page is a page that outputs the contents of the shortcode.
I'm using output buffering around get_template_part() for two reasons: 1. So I only have one instance of the template in my code - and - 2. Because the template is actually pretty substantial and appending all of it to an output variable would be a daunting task.
The shortcode works fine in every way except when saving the page.
Here is a video demonstrating the issue:
https://www.awesomescreenshot.com/video/1146323?key=103ae00d841b47cee8a902eb18c8988a
Here is my code:
function get_main_page_content( $atts ) {
$main_page_id = $atts['main_page_id'];
$section = $atts['section'];
$people_display_option = $atts['people_display_option'];
$GLOBALS['sc_display_option'] = $people_display_option;
ob_start();
if(have_rows('flexible_content', $main_page_id)):
while(have_rows('flexible_content', $main_page_id)): the_row();
if ( $section == 'agenda' ) {
get_template_part('partials/agenda');
}
if ( $section == 'people_cards' ) {
get_template_part('partials/people-cards');
}
endwhile;
endif;
ob_end_flush();
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode('get_main_page_content', 'get_main_page_content');
It looks to me like ob_end_flush() is not needed and is redundant. That might be causing the OB to send twice, resulting in that code on your screen.
I'd be curious if your problem persists if you drop that line. Also, for a very simplified version of your exact usecase, check this blog post:
https://konstantin.blog/2013/get_template_part-within-shortcodes
I am using Wordpress 4.3.1 and I noticed that it used oEmbed to automatically embed my tracks from soundcloud.
This is awesome! Except, I have no control over how the widget is displayed.
First of all I would like to display the "Classic Embed" without the artwork.
Secondly I want the widget to be wider.
How can I achieve this without editing the shortcode for each post in my wordpress page?
You can add a filter to change the way the oEmbed code is displayed. Here is something to get you started:
// this function is called on all urls surrounded by the WordPress embed shortcode.
// i.e.: [embed]https://soundcloud.com/gratefuldead/grateful-dead-box-of-rain[/embed]
function my_embed_options( $code ) {
// look for SoundCloud link:
if( strpos( $code, 'soundcloud.com') !== false ) {
$code = str_replace( 'show_artwork=true', 'show_artwork=false', $code );
}
return $code;
}
add_filter( 'embed_oembed_html', 'my_embed_options' );
You can find full parameter list for the SoundCloud player here.
I have created a small wordpress plugin that displays a list of people in a page via shortcode.
When the user clicks on one of the names from the list, a query_var gets set and my plugin catches the $_GET with the specific id of the person the user just clicked. All very well until now.
My problem is that now I want to display a page with the details (for the clicked element) but I dont seem to be able to edit the content or post that gets to the page and it returns me to the page with the list of people.
My question is how do I edit the post? I have tried adding a add_filter('the_content','my_func') to this, but this does not work since this hook is probably already passed.
I can access the post directly via get_content() or get_post(), but I dont seem to be able to make the page populated new data.
In other words... this does nore seem to work
$fid = $_GET['fid'];
global $wpdb;
$sql = "select * from fighters where fighter_id = {$fid} limit 1";
$fighter = $wpdb->get_row($sql);
$html = $this->_getFighterPageLayout($fighter);
$post = get_post();
$post->post_content = $html;
$post->title = 'test';
$post->private = false;
// or even just global $content = $html;
What am I doing wrong and what ways do I have to edit/update the content/post?
You have to use the hooks of Wordpress to update the content. This works with the add_filter function
Try something like this, it should works
function mytheme_content_filter( $content ) {
// Do stuff to $content, which contains the_content()
// Then return it
return $content;
}
add_filter( 'the_content', 'mytheme_content_filter' );
hHi all! I have posted this question on the WP support forums, but the community doesn't seem to be as active as stack's, so I am taking a chance here!
I am looking for a plugin that would automatically create a navigation menu (through the use of shortcodes for example) on a long single page documentation page.
The long page is divided into sections. I can imagine using a shortcode at the beginning of every section, and this will create a menu that would be displayed in a sidebar for example (called through a second shortcode perhaps, or a widget)
Any thoughts? Advice?
Thanks!
Use [section]Section Title[/section] shortcodes, then [section_navigation] where you want the navigation links output.
This works, but with a massive caveat -- that [section_navigation] needs to be in your post/page after the other [section] shortcodes... otherwise it generates an empty list.
You should be ok to use it in your theme by putting <?php echo do_shortcode("[section_navigation]");?> in sidebar.php. It will work as long as get_sidebar() is after the_content() in your theme templates (it usually is).
This to go in functions.php
$whit_sections = "";
// [section]My Section Title[/section]
function whit_section_shortcode( $atts, $title = null ) {
// $content is the title you have between your [section] and [/section]
$id = urlencode(strip_tags($title));
// strip_tags removes any formatting (like <em> etc) from the title.
// Then urlencode replaces spaces and so on.
global $whit_sections;
$whit_sections .= '<li>'.$title.'</li>';
return '<span id="'.$id.'">'.$title.'</span>';
}
add_shortcode('section', 'whit_section_shortcode');
// [section_navigation]
function whit_section_navigation_shortcode( $atts, $title = null ) {
global $whit_sections;
return '<ul class="section-navigation">'.$whit_sections.'</ul>';
}
add_shortcode('section_navigation', 'whit_section_navigation_shortcode');
I am editing a Custom Post Type template, and am using custom fields to enter info into a meta box to be included on the page, as well as include some static default text on all the pages.
I basically need to "chunk" together the post info in the_content along with the static text and some meta box info. Here's what I want:
the_content
static text
meta box 1
more static text
meta box 2
end of the _content
I have plugins that add social buttons before the_content and a signature after the_content so I am trying to figure out how to get all my custom stuff sandwiched in between those.
If I just add the meta boxes i nthe template, they display outside of the_content and the plugins display in unwanted places.
I ended up figuring this out on my own. The solution: using functions.php and add_filter, I had to create a new function to create the default content, and it works great.
here's the general code for anyone interested:
function custom_post_type_default_content($content) {
global $post;
if ($post->post_type == 'your-custom-post-type') {
$content .= '<p> '. get_post_meta( $post->ID, "metabox-1-slug", true ).'
<br />
<p> '. get_post_meta( $post->ID, "metabox-2-slug", true ).'</p>
<p>YOUR TEXT HERE.</p>';
}
add_filter('the_content', 'custom_post_type_default_content', 0);
Note that the zero just near the end controls placement. I have a social media plugin that has a priority of "1", and to get the default content to appear above that I have to make this a priority of "0".
Also note the single apostrophes that open and close the code following $content .=
You basically add whatever you want between those apostrophes, and in this case I am pulling metabox info which have their own apostrophes containing code. It gets confusing!
In other words, your code should be $content .='YOUR CUSTOM CONTENT' and within those apostrophes, add your text, code, etc. The standalone metabox code is '. get_post_meta( $post->ID, "metabox-1-slug", true ).' which is nested inside where the YOUR CUSTOM CONTENT text is.
I am basically explaining this to myself, as these were the things that tripped me up so figured would explain them in detail to help someone else like me. Or me when I have to go look this up again!
Post your single.php here or on pastebin along with the custom field names you're using (& where you want them) and I'll try to help you figure out what you want.
Copy this code to your function.php file.
function content_function_update($content) {
global $wp_query, $post;
if ($post->post_type == 'your-custom-post-type') {
$postid = $wp_query->post->ID;
$value1 = "your value 1";
if($value1 !== '') {
$content = $content . "<br>" . $value1
}
else {
$content = $content;
}
}
return $content;
}
add_filter('the_content', 'content_function_update');
Add any custom content in variables and append it to $content variable.