Single Page Navigation Menu Dynamically Generated - wordpress

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');

Related

Wordpress Shortcode Output Buffering Renders Content When Saving Post in WP Admin

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

how to create multiple shortcodes in Wordpress and selectively position them anywhere in a custom template page file

In my functions file I have this:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );
In the CMS page editor I have this:
[caption]My Caption[/caption]
This page is utilizing a custom template file template-mypage.php. My question is: I would like to create multiple short codes types within the CMS such as:
[caption1]My Caption[/caption1]
[caption2]My Caption[/caption2]
[caption3]My Caption[/caption3]
then in my template-mypage.php... I would like to selectively choose where to place [caption1], [caption2], [caption3]... for example [caption1] will go somewhere on the top... [caption2] in the middle and [caption3] towards the bottom of the template-mypage.php, all seperated by some huge chunks of HTML content. I do not want to write any HTML within the WP CMS... all HTML should be written in the template-mypage.php.
Currently I believe WP limits shortcode output to come out of the_content(); Is it possible to do something like the_content_of_caption1(), the_content_of_caption2(), the_content_of_caption3()?
Thanks please let me know!
this product does this perfectly
http://wordpress.org/plugins/multiple-content-blocks/

Get WordPress Sidebars Fom The Current Front End

Is there a function to retrieve the WordPress sidebar(s) that is displayed in the current front end?
I posted a solution that might work, here: Checking If A WordPress Widget Displayed In The Current Front End
dynamic_sidebar doesn't register that it's been called for that and that sidebar. Neither is there a suitable hook to do this yourself. So I'm afraid you would have to tell Wordpress for each template what sidebars are being displayed. One way to do this would be to create a wrapper function like this
function wrap_dynamic_sidebar( $sidebar_id )
{
global $sidebars_in_this_template;
$sidebars_in_this_template[] = $sidebar_id;
return dynamic_sidebar( $sidebar_id );
}
And replace dynamic_sidebar with this everywhere (but I understand that it is very likely this solution will not be feasible for you).
If you want to display a list of all sidebars, you could use $wp_registered_sidebars
global $wp_registered_sidebars;
$sidebar_ids = array_keys( $wp_registered_sidebars );

Including Custom Fields & static text in the_content in WordPress

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.

Adding custom tags in Wordpress

I'm creating a new WP theme and I would like to allow the user to insert a divider in between paragraphs or images he/she is entering, for a post/page.
I want the output to be something like:
<div class="divider"></div>
But I don't want the user to have to enter HTML in the WYSIWYG editor. Is it possible to ask them to enter something like:
<-- break -->
and then translate that to the div markup on display?
Thanks.
Build a function in your theme's functions.php file like this:
function add_div( $content ) {
$content = str_replace( '<!-- break -->', '<div class="divider"></div>', $content );
return $content;
}
then add the following to the theme:
add_filter( "the_content", "add_div" );
The function uses PHP's string replace function to find the text you want your users to input and replace it with the text you want to render, the add_filter() function uses Wordpress's content filter to apply your function to the content of each post after it is read from the database, but before it is rendered to the browser.
This will work in PHP4 and up, which is still the official level of support for Wordpress.

Resources