Adding custom tags in Wordpress - 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.

Related

How to change text in editor of wordpress cms

I want to edit the text before publication but with one condition:
the text in my editor also be edited.
A simple example:
function edit_content($the_content){
return $the_content.'ali';
}
The problem with this code: it properly read the edited text but it does not change the text in the editor.
How can I edit the text in the editor?
Use Filter Hook content_save_pre
If you want to change the content just before it saved to database, you can use this filter hook:
function my_filter_function_name( $content ) {
// do something with $content
return $content;
}
add_filter( 'content_save_pre', 'my_filter_function_name', 10, 1 );

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/

How to disable a sidebar in wordpress

I am using a wordpress theme with two sidebars. I want to disable one for them for a specific page but the code I found regarding disabling sidebar by applying check on function:
<?php get_sidebar() ?>
It will disable both of them. How can I disable one of them only. with the other sidebar working.
Please help!!!!
You can simply for example
<?php if (!is_page('about-me')) get_sidebar(); ?>
This will disable sidebar on about me page. If you put this on page.php (if have any) otherwise put this on index.php. Here 'about-me' is page slug but you can also use page id like is_page(5) and as well as page title too. To check multiple page using slug, id and title you can use an array like
is_page(array(42,'about-me','Contact'));
For more about is_page() function see http://codex.wordpress.org/Function_Reference/is_page
Or using filter in functions.php, simply put this in functions.php
function disable_footer_widgets( $sidebars_widgets )
{
if (is_single())
{
$sidebars_widgets['footer'] = false;
}
return $sidebars_widgets;
}
add_filter( 'sidebars_widgets', 'disable_footer_widgets' );
This is just an example, you have to change widget name.

Single Page Navigation Menu Dynamically Generated

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

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.

Resources