Custom shortcode for creating tabs - php

I am currently trying to create a shortcode for WordPress that creates tabs, I know there are available shortcodes.. But I am trying to learn how to write them myself.
Currently I am generating the tab menu items, but i am stuck with placing the content in an entirely new div.
Front end Usage:
[tabs]
[tab title="tab" active="y" id="home"]home[/tab]
[tab title="tab2" active="n" id="about"]about[/tab]
[tab title="tab3" active="n" id="help"]help[/tab]
[/tabs]
And then the code:
function tabs_group( $atts, $content = null ) {
extract(shortcode_atts(array(
'id' => '',
'class' => ''
), $atts));
$output = '<ul class="nav nav-tabs '.$class.'" ';
if(!empty($id))
$output .= 'id="'.$id.'"';
$output .='>'.do_shortcode($content).'</ul>';
return $output;
}
add_shortcode("tabs", "tabs_group");
function tab($atts, $content = null) {
extract(shortcode_atts(array(
'id' => '',
'title' => '',
'active'=>'n'
), $atts));
if(empty($id))
$id = 'tab_item_'.rand(100,999);
$output = '<li class="'.($active == 'y' ? 'active' :'').'">
'.$title.'
</li>';
$output .= '<div class="tab-content">
<div class="tab-pane active" id="'.$id.'">'.$content.'</div>
<div class="tab-pane" id="'.$id.'">'.$content.'</div>
<div class="tab-pane" id="'.$id.'">'.$content.'</div>
</div>';
return $output;
}
add_shortcode("tab", "tab");
it currently returns:
<ul class="nav nav-tabs">
<li class="active">
tab
</li><div class="tab-content">
<div class="tab-pane active" id="home">home</div>
<div class="tab-pane" id="home">home</div>
<div class="tab-pane" id="home">home</div>
</div>
<li class="">
tab2
</li><div class="tab-content">
<div class="tab-pane active" id="about">about</div>
<div class="tab-pane" id="about">about</div>
<div class="tab-pane" id="about">about</div>
</div>
<li class="">
tab3
</li><div class="tab-content">
<div class="tab-pane active" id="help">help</div>
<div class="tab-pane" id="help">help</div>
<div class="tab-pane" id="help">help</div>
</div>
</ul>
and i need it to return:
<ul class="nav nav-tabs " >
<li class="active">
tab
</li>
<li class="">
tab2
</li>
<li class="">
tab3
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="home">blah</div>
<div class="tab-pane" id="about">blah</div>
<div class="tab-pane" id="help">blah</div>
</div>
I have added to the output, thank you Obmerk Kronen.
Any Help Greatly Appreciated.

The problem here is that you need to add your divs after do_shortcode($content) : while parsing tab shorcodes, you need to keep your divs somewhere to be able to output them after.
A simple way (not nicest way) for doing this could be using a global var, e.g. :
add_shortcode('tabs', 'tabs_group');
add_shortcode('tab', 'tab');
// this variable will hold your divs
$tabs_divs = '';
function tabs_group( $atts, $content = null ) {
global $tabs_divs;
// reset divs
$tabs_divs = '';
extract(shortcode_atts(array(
'id' => '',
'class' => ''
), $atts));
$output = '<ul class="nav nav-tabs '.$class.'" ';
if(!empty($id))
$output .= 'id="'.$id.'"';
$output.='>'.do_shortcode($content).'</ul>';
$output.= '<div class="tab-content">'.$tabs_divs.'</div>';
return $output;
}
function tab($atts, $content = null) {
global $tabs_divs;
extract(shortcode_atts(array(
'id' => '',
'title' => '',
'active'=>'n'
), $atts));
if(empty($id))
$id = 'tab_item_'.rand(100,999);
$activeClass = $active == 'y' ? 'active' :'';
$output = '
<li class="'.$activeClass.'">
'.$title.'
</li>
';
$tabs_divs.= '<div class="tab-pane '.$activeClass.'" id="'.$id.'">'.$content.'</div>';
return $output;
}

you do not currently have the content part of the divs in your content
$output .= '<div class="tab-content">
<div class="tab-pane active" id="'.$id.'">'.$title.'</div>
<div class="tab-pane" id="'.$id.'">'.$title.'</div>
<div class="tab-pane" id="'.$id.'">'.$title.'</div>
</div>'

<?php
// my shortcode look like this.
[tabgroup]
[tab title="Your title #1" class="active" ]
Your Description here #1
[/tab]
[tab title="Your title #2" ]
Your Description here #2
[/tab]
[tab title="Your title #3" ]
Your Description here #3
[/tab]
[/tabgroup]
add_shortcode( 'tab', 'ease_tabs' );
function ease_tabs( $atts, $content ){
extract(shortcode_atts(array(
'title' => 'Tab',
'class' => false,
), $atts));
$x = isset($GLOBALS['tab_count'])?$GLOBALS['tab_count']:0;
$GLOBALS['tab_count'] = isset($GLOBALS['tab_count'])?$GLOBALS['tab_count']:0;
$GLOBALS['tabs'][$GLOBALS['tabs_count']][$x] = array(
'title' => $title ,
'class' => $class ,
'content' => $content ,
);
$GLOBALS['tab_count']++;
}
add_shortcode( 'tabgroup', 'ease_tabgroup' );
function ease_tabgroup( $atts, $content ){
if( isset( $GLOBALS['tabs_count'] ) )
$GLOBALS['tabs_count']++;
else
$GLOBALS['tabs_count'] = 0;
do_shortcode( $content );
if( is_array( $GLOBALS['tabs'][$GLOBALS['tabs_count']] ) ){
$tabs=array();
$panes=array();
foreach( $GLOBALS['tabs'][$GLOBALS['tabs_count']] as $tab ){
$panes_class = ( !empty($tab["class"]) && $tab['class'] == "active" ) ? 'tab-pane active' : 'tab-pane';
$__title = preg_replace('/[^a-zA-Z0-9._\-]/', '', strtolower($tab['title']) );
$tabs[] = '<li role="presentation" class="'.$tab['class'].'" >
'.$tab['title'].'
</li>';
$panes[] = sprintf( '<div class="%s" id="%s"> %s </div>',$panes_class, $__title, $tab['content'] );
}
$return = "\n".'<div role="tabpanel">
<ul role="tablist" class="nav nav-tabs">'
.implode( "\n", $tabs ).
'</ul>'."\n".
'<div class="tab-content">'
.implode( "\n", $panes ).
'</div>
</div>'."\n";
}
return do_shortcode( sprintf('<div class="tp-tabs"> %s </div>', $return));
}
wordpressshortcode

Related

Wordpress Nested Shortcode. Attributes get truncated

I have this code that I made for wordpress inside functions.php
function card_shortcode($atts, $content = null) {
$attributes = shortcode_atts( array(
'titolo' => '',
'prezzo' => '0',
'bullet' => ''
), $atts );
$title = esc_attr($attributes['titolo']);
$price = esc_attr($attributes['prezzo']);
$bullet = esc_attr($attributes['bullet']);
$output = '<div class="card">
<div class="card_main_point">
[card_title titolo="'.$title.'"]
[card_price prezzo="'.$price.'€"]
</div>
<div class="card_secondary_point">
<ul class="bullet_holder">
[card_bullet bullet="'.$bullet .'"]
</ul>
</div>
</div>
[card_style]';
return do_shortcode($output);
}
I tried looking for answer in the internet but got no result as the output that comes out is this
<div class="card">
<div class="card_main_point">[card_titleTest][card_price42€]
<div class="card_price">
<h4></h4>
</div>
</div>
<div class="card_secondary_point">
<ul class="bullet_holder">[card_bulletrew;tre;ytr]
</ul>
</div>
</div>
I've also tried this solution but with the same outcome. It's like that shortcode that have attributes get trucated and only the variable remains. Can someone help me?
function card_shortcode($atts, $content = null) {
$attributes = shortcode_atts( array(
'titolo' => '',
'prezzo' => '0',
'bullet' => ''
), $atts );
$title = esc_attr($attributes['titolo'];
$price = esc_attr($attributes['prezzo'];
$bullet = esc_attr($attributes['bullet'];
$output = '<div class="card">
<div class="card_main_point">'
. do_shortcode('[card_title titolo="'.$title.'"]') . ''
. do_shortcode('[card_price prezzo="'.$price.'€"]') .'
</div>
<div class="card_secondary_point">
<ul class="bullet_holder">'
. do_shortcode('[card_bullet bullet="'.$bullet .'"]') . '
</ul>
</div>
</div>
'
. do_shortcode("[card_style]");
return do_shortcode($output);
}
I've resolved. I had to clean the cache and on my last 2 tries I accidentally update the them style instead of the theme functions. If it is of help the topmost solution works

Menu and submenu PHP for Wordpress

I have a HTML structure and I want to change those menu elements for a WordPress Menu maintaining the class and id's, and I want to change these elements for the ones I obtain using wp_get_nav_menu_items() and for each, but don't know how.
The thing is changing all Categories for parent and subitem WP menu category and so on, until 5 level of depth that has the menu. For example, replace Devices with first item depth 0, inside change Mobile Phones for second level, and go changing.
<!-- mp-menu -->
<nav id="mp-menu" class="mp-menu mp-overlap">
<div class="mp-level mp-level-open" data-level="1">
<h2 class="icon icon-world">All Categories</h2>
<ul>
<li class="icon icon-arrow-left">
<a class="icon icon-display" href="#">Devices</a>
<div class="mp-level" data-level="2">
<h2 class="icon icon-display">Devices</h2>
<a class="mp-back" href="#">back</a>
<ul>
<li class="icon icon-arrow-left">
<a class="icon icon-phone" href="#">Mobile Phones</a>
<div class="mp-level">
<h2>Mobile Phones</h2>
<a class="mp-back" href="#">back</a>
<ul>
<li>Super Smart Phone</li>
<li>Thin Magic Mobile</li>
<li>Performance Crusher</li>
<li>Futuristic Experience</li>
</ul>
</div>
</li>
<li class="icon icon-arrow-left">
<a class="icon icon-tv" href="#">Televisions</a>
<div class="mp-level">
<h2>Televisions</h2>
<a class="mp-back" href="#">back</a>
<ul>
<li>Flat Superscreen</li>
<li>Gigantic LED</li>
<li>Power Eater</li>
<li>3D Experience</li>
<li>Classic Comfort</li>
</ul>
</div>
</li>
<li class="icon icon-arrow-left">
<a class="icon icon-camera" href="#">Cameras</a>
<div class="mp-level">
<h2>Cameras</h2>
<a class="mp-back" href="#">back</a>
<ul>
<li>Smart Shot</li>
<li>Power Shooter</li>
<li>Easy Photo Maker</li>
<li>Super Pixel</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li class="icon icon-arrow-left">
<a class="icon icon-news" href="#">Magazines</a>
<div class="mp-level">
<h2 class="icon icon-news">Magazines</h2>
<a class="mp-back" href="#">back</a>
<ul>
<li>National Geographic</li>
<li>Scientific American</li>
<li>The Spectator</li>
<li>The Rambler</li>
<li>Physics World</li>
<li>The New Scientist</li>
</ul>
</div>
</li>
<li class="icon icon-arrow-left">
<a class="icon icon-shop" href="#">Store</a>
<div class="mp-level">
<h2 class="icon icon-shop">Store</h2>
<a class="mp-back" href="#">back</a>
<ul>
<li class="icon icon-arrow-left">
<a class="icon icon-t-shirt" href="#">Clothes</a>
<div class="mp-level">
<h2 class="icon icon-t-shirt">Clothes</h2>
<a class="mp-back" href="#">back</a>
<ul>
<li class="icon icon-arrow-left">
<a class="icon icon-female" href="#">Women's Clothing</a>
<div class="mp-level">
<h2 class="icon icon-female">Women's Clothing</h2>
<a class="mp-back" href="#">back</a>
<ul>
<li>Tops</li>
<li>Dresses</li>
<li>Trousers</li>
<li>Shoes</li>
<li>Sale</li>
</ul>
</div>
</li>
<li class="icon icon-arrow-left">
<a class="icon icon-male" href="#">Men's Clothing</a>
<div class="mp-level">
<h2 class="icon icon-male">Men's Clothing</h2>
<a class="mp-back" href="#">back</a>
<ul>
<li>Shirts</li>
<li>Trousers</li>
<li>Shoes</li>
<li>Sale</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li>
<a class="icon icon-diamond" href="#">Jewelry</a>
</li>
<li>
<a class="icon icon-music" href="#">Music</a>
</li>
<li>
<a class="icon icon-food" href="#">Grocery</a>
</li>
</ul>
</div>
</li>
<li><a class="icon icon-photo" href="#">Collections</a></li>
<li><a class="icon icon-wallet" href="#">Credits</a></li>
</ul>
</div>
</nav>
<!-- /mp-menu -->
you can customize nav menu walker class of wordpress to put your N level HTML menu structure. Hers is a example of bootstrap nav walker
if (!class_exists('BootstrapBasicMyWalkerNavMenu')) {
class BootstrapBasicMyWalkerNavMenu extends Walker_Nav_Menu
{
//Overwrite display_element function to add has_children attribute. Not needed in >= Wordpress 3.4
/**
* #link https://gist.github.com/duanecilliers/1817371 copy from this url
*/
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output)
{
if (!$element)
return;
$id_field = $this->db_fields['id'];
//display this element
if (is_array($args[0]))
$args[0]['has_children'] = !empty($children_elements[$element->$id_field]);
else if (is_object($args[0]))
$args[0]->has_children = !empty($children_elements[$element->$id_field]);
$cb_args = array_merge(array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'start_el'), $cb_args);
$id = $element->$id_field;
// descend only when the depth is right and there are childrens for this element
if (($max_depth == 0 || $max_depth > $depth + 1) && isset($children_elements[$id])) {
foreach ($children_elements[$id] as $child) {
if (!isset($newlevel)) {
$newlevel = true;
//start the child delimiter
$cb_args = array_merge(array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
}
$this->display_element($child, $children_elements, $max_depth, $depth + 1, $args, $output);
}
unset($children_elements[$id]);
}
if (isset($newlevel) && $newlevel) {
//end the child delimiter
$cb_args = array_merge(array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
}
//end this element
$cb_args = array_merge(array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'end_el'), $cb_args);
}// display_element
/**
* #link https://gist.github.com/duanecilliers/1817371 copy from this url
*/
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
{
if ((is_object($item) && $item->title == null) || (!is_object($item))) {
return ;
}
$indent = ($depth) ? str_repeat("\t", $depth) : '';
$li_attributes = '';
$class_names = $value = '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
//Add class and attribute to LI element that contains a submenu UL.
if (is_object($args) && $args->has_children) {
//$classes[] = 'dropdown';
$li_attributes .= ' data-dropdown="dropdown"';
}
$classes[] = 'menu-item-' . $item->ID;
//If we are on the current page, add the active class to that menu item.
$classes[] = ($item->current) ? 'active' : '';
//Make sure you still add all of the WordPress classes.
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
$class_names = ' class="' . esc_attr($class_names) . '"';
$id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
$id = strlen($id) ? ' id="' . esc_attr($id) . '"' : '';
$output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
//Add attributes to link element.
$attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
$attributes .=!empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
$attributes .=!empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
$attributes .=!empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';
$attributes .= (is_object($args) && $args->has_children) ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';
$item_output = (is_object($args)) ? $args->before : '';
$item_output .= '<a' . $attributes . '>';
$item_output .= (is_object($args) ? $args->link_before : '') . apply_filters('the_title', $item->title, $item->ID) . (is_object($args) ? $args->link_after : '');
$item_output .= (is_object($args) && $args->has_children) ? ' <span class="caret"></span> ' : '';
$item_output .= '</a>';
$item_output .= (is_object($args) ? $args->after : '');
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}// start_el
public function start_lvl(&$output, $depth = 0, $args = array())
{
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu dropdown-menu\">\n";
}
}
}
How to use this,
<?php wp_nav_menu(array('theme_location' => 'your-menu-location', 'container' => false, 'menu_class' => 'nav navbar-nav', 'walker' => new BootstrapBasicMyWalkerNavMenu())); ?>
Hope this will helps you.
For more information,
Understanding the Walker Class
WP Bootstrap Navwalker
Custom Nav Menu Walker Function

Variable appearing above html in php when using return

I am trying to put the variable $about inside of the html I am returning...however when I view the output, it puts $about above the html code...
function cc_shortcode( $atts ) {
$other_page = 498;
$about = the_field('cc_about_this_course', $other_page);
extract( shortcode_atts( array(
'id' => '',
), $atts) );
$result = '<div class="main">
<div class="accordion">
<div class="accordion-section">
<a class="accordion-section-title active" href="#accordion-1">ABOUT THIS COURSE<i class="fa fa-arrow-down"></i></a>
<div id="accordion-1" class="accordion-section-content open" style="display: block;">
<p>' . $about . '</p>
</div>
</div>
';
return $result;
}
https://www.advancedcustomfields.com/resources/the_field/
the_field used for displaying value from the current post that's why it display value..
solution to use get_field ; https://www.advancedcustomfields.com/resources/get_field/
if the there is not get_field.. (for common problems)... like this..
you can ob_start(); and ob_get_clean for you problem
function cc_shortcode( $atts ) {
$other_page = 498;
ob_start();
the_field('cc_about_this_course', $other_page);
$about = ob_get_clean();
extract( shortcode_atts( array(
'id' => '',
), $atts) );
$result = '<div class="main">
<div class="accordion">
<div class="accordion-section">
<a class="accordion-section-title active" href="#accordion-1">ABOUT THIS COURSE<i class="fa fa-arrow-down"></i></a>
<div id="accordion-1" class="accordion-section-content open" style="display: block;">
<p>' . $about . '</p>
</div>
</div>
';
return $result;
}

Create wordpress shortcode for jQuery UI Tabs

I Am trying to create a wordpress shortcode that allows users to creat jQuery UI tabs. I found this great script online which claims to do it - however it has some quite unwelcome results in my page.
Firstly, the script I am currently using is as follows:
add_shortcode( 'tabgroup', 'jqtools_tab_group' );
function jqtools_tab_group( $atts, $content )
{
$GLOBALS['tab_count'] = 0;
do_shortcode( $content );
if( is_array( $GLOBALS['tabs'] ) ){
foreach( $GLOBALS['tabs'] as $tab ){
$tabs[] = '<li><a class="" href="#">'.$tab['title'].'</a></li>';
$panes[] = '<div class="pane"><h3>'.$tab['title'].'</h3>'.$tab['content'].'</div>';
}
$return = "\n".'<!-- the tabs --><ul class="tabs">'.implode( "\n", $tabs ).'</ul>'."\n".'<!-- tab "panes" --><div class="panes">'.implode( "\n", $panes ).'</div>'."\n";
}
return $return;
}
add_shortcode( 'tab', 'jqtools_tab' );
function jqtools_tab( $atts, $content )
{
extract(shortcode_atts(array(
'title' => 'Tab %d'
), $atts));
$x = $GLOBALS['tab_count'];
$GLOBALS['tabs'][$x] = array( 'title' => sprintf( $title, $GLOBALS['tab_count'] ), 'content' => $content );
$GLOBALS['tab_count']++;
}
However, the HTML output I am TRYING to achieve is this:
<div class="tabs">
<ul>
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
<div id="tabs-1">
<p>Content</p>
</div>
<div id="tabs-2">
<p>Content</p>
</div>
<div id="tabs-3">
<p>Content</p>
</div>
</div>
The shortcode php gives a slightly different output to what I need in order to make this work - here is my current outputted HTML:
<!-- the tabs -->
<ul class="tabs">
<li><a class="" href="#">Tab 0</a></li>
<li><a class="" href="#">Tab 1</a></li>
<li><a class="" href="#">Tab 2</a></li>
</ul>
<!-- tab "panes" -->
<div class="panes">
<div class="pane">
<h3>Tab 0</h3>Content
</div>
<div class="pane">
<h3>Tab 1</h3>Content
</div>
<div class="pane">
<h3>Tab 2</h3>Content
</div>
</div>
Finally, the shortcode I am using looks like this:
[tabgroup]
[tab title="tab1"]Content[/tab]
[tab title="tab2"]Content[/tab]
[tab title="tab3"]Content[/tab]
[/tabgroup]
My question is, how do I need to change my php code to make the outputted HTML look like the HTML needed to make the tabs work?
OK this is what you have to do.
if( is_array( $GLOBALS['tabs'] ) ){
foreach( $GLOBALS['tabs'] as $k=>$tab ){
$tabs[] = '<li>'.$tab['title'].'</li>';
$panes[] = '<div id="tab-'.$k.'"><p>'.$tab['content'].'</p></div>';
}
$return = "\n".'<!-- the tabs --><div class="tabs"><ul>'.implode( "\n", $tabs ).'</ul>'."\n".'<!-- tab "panes" -->'.implode( "\n", $panes ).'</div>'."\n";
}
You can also add shortcode like this:-
// Tab
function tab_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'title' => ''
), $atts ) );
return '<div id="tabs-'. sanitize_title( $title ) .'">'. do_shortcode( $content ) .'</div>';
}
add_shortcode( 'tab', 'tab_shortcode' );
// Tabs Container
function tabs_container_shortcode( $atts, $content = null ) {
preg_match_all( '/tab title="([^\"]+)"/i', $content, $matche, PREG_OFFSET_CAPTURE );
$tab_title = array();
if( isset($matche[1]) ) {
$tab_title = $matche[1];
}
$output = '';
if( count($tab_title) ) {
$output .= '<div id="tabs">';
$output .= '<ul class="nav clearfix">';
foreach( $tab_title as $tab ){
$output .= '<li>' . $tab[0] . '</li>';
}
$output .= '</ul>' . do_shortcode( $content ) . '</div>';
} else {
$output .= do_shortcode( $content );
}
return $output;
}
add_shortcode( 'tabs_container', 'tabs_container_shortcode' );
This will also work for tabs. Hope this will help you bit.

php - custom variable href value does not work

I am editing the code page of a wordpress post.
Now here is the full page code:
<?php
add_action( 'init', 'create_recipes' );
function create_recipes() {
//$portfolio_translation = get_option(THEME_NAME_S.'_cp_portfolio_slug','portfolio');
$labels = array(
'name' => _x('Recipes', 'Recipe General Name', 'crunchpress'),
'singular_name' => _x('Recipe Item', 'Recipe Singular Name', 'crunchpress'),
'add_new' => _x('Add New', 'Add New Event Name', 'crunchpress'),
'add_new_item' => __('Add New Recipe', 'crunchpress'),
'edit_item' => __('Edit Recipe', 'crunchpress'),
'new_item' => __('New Recipe', 'crunchpress'),
'view_item' => __('View Recipe', 'crunchpress'),
'search_items' => __('Search Recipe', 'crunchpress'),
'not_found' => __('Nothing found', 'crunchpress'),
'not_found_in_trash' => __('Nothing found in Trash', 'crunchpress'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => CP_PATH_URL . '/framework/images/recipe-icon.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title','editor','author','thumbnail','excerpt','comments'),
'rewrite' => array('slug' => 'recipes', 'with_front' => false)
);
register_post_type( 'recipes' , $args);
register_taxonomy(
"recipe-category", array("recipes"), array(
"hierarchical" => true,
"label" => "Recipe Categories",
"singular_label" => "Recipe Categories",
"rewrite" => true));
register_taxonomy_for_object_type('recipe-category', 'recipes');
register_taxonomy(
"recipe-tag", array("recipes"), array(
"hierarchical" => false,
"label" => "Recipe Tag",
"singular_label" => "Recipe Tag",
"rewrite" => true));
register_taxonomy_for_object_type('recipe-tag', 'recipes');
}
add_action('add_meta_boxes', 'add_recipes_option');
function add_recipes_option(){
add_meta_box('recipe-option', __('Recipes Options','crunchpress'), 'add_recipe_option_element',
'recipes', 'normal', 'high');
}
function add_recipe_option_element(){
$recipe_price = '';
$recipe_social = '';
$sidebars = '';
$right_sidebar_recipe = '';
$left_sidebar_recipe = '';
$recipe_detail_xml = '';
$select_chef = '';
$recipe_url = '';
foreach($_REQUEST as $keys=>$values){
$$keys = $values;
}
global $post;
$recipe_detail_xml = get_post_meta($post->ID, 'recipe_detail_xml', true);
$ingredients_settings = get_post_meta($post->ID, 'ingredients_settings', true);
$nutrition_settings = get_post_meta($post->ID, 'nutrition_settings', true);
if($recipe_detail_xml <> ''){
$cp_recipe_xml = new DOMDocument ();
$cp_recipe_xml->loadXML ( $recipe_detail_xml );
$recipe_price = find_xml_value($cp_recipe_xml->documentElement,'recipe_price');
$recipe_url = find_xml_value($cp_recipe_xml->documentElement,'recipe_url');
$recipe_social = find_xml_value($cp_recipe_xml->documentElement,'recipe_social');
$sidebars = find_xml_value($cp_recipe_xml->documentElement,'sidebars');
$left_sidebar_recipe = find_xml_value($cp_recipe_xml->documentElement,'left_sidebar_recipe');
$right_sidebar_recipe = find_xml_value($cp_recipe_xml->documentElement,'right_sidebar_recipe');
}
?>
<div class="event_options">
<ul class="recipe_class top-bg">
<li><h2>Recipe Options and Social Sharing</h2></li>
</ul>
<ul class="recipe_class">
<li class="panel-title">
<label for="recipe_social" > <?php _e('SOCIAL NETWORKING', 'crunchpress'); ?> </label>
</li>
<li class="panel-input">
<label for="recipe_social"><div class="checkbox-switch <?php
echo ($recipe_social=='enable' || ($recipe_social=='' && empty($default)))? 'checkbox-switch-on': 'checkbox-switch-off';
?>"></div></label>
<input type="checkbox" name="recipe_social" class="checkbox-switch" value="disable" checked>
<input type="checkbox" name="recipe_social" id="recipe_social" class="checkbox-switch" value="enable" <?php
echo ($recipe_social=='enable' || ($recipe_social=='' && empty($default)))? 'checked': '';
?>>
</li>
<li class="description"><p>Turn On/Off Social Sharing on Event Detail.</p></li>
</ul>
<div class="clear"></div>
<ul class="recipe_class">
<li class="panel-title">
<label for="recipe_price" > <?php _e('RECIPE PRICE', 'crunchpress'); ?> </label>
</li>
<li class="panel-input">
<input type="text" name="recipe_price" id="recipe_price" value="<?php if($recipe_price <> ''){echo $recipe_price;};?>" />
</li>
<li class="description"><p>Please enter your recipe price.</p></li>
</ul>
<div class="clear"></div>
<!--david-->
<ul class="recipe_class">
<li class="panel-title">
<label for="recipe_url" > <?php _e('RECIPE URL', 'crunchpress'); ?> </label>
</li>
<li class="panel-input">
<input type="text" name="recipe_url" id="recipe_url" value="<?php if($recipe_url <> ''){echo $recipe_url;};?>" />
</li>
<li class="description"><p>Please enter your url</p></li>
</ul>
<div class="clear"></div>
<!--david end-->
<?php echo show_sidebar($sidebars,'right_sidebar_recipe','left_sidebar_recipe',$right_sidebar_recipe,$left_sidebar_recipe);?>
<ul class="recipe_class">
<li class="panel-title">
<label><?php _e('Add Ingredients', 'crunchpress'); ?></label>
</li>
<li class="panel-input">
<input type="text" id="add-more-ingre" value="type title here" rel="type title here">
<div id="add-more-ingre" class="add-more-ingre"></div>
</li>
<li class="description"><p>Add Ingredients for this recipe.</p></li>
<div class="clear"></div>
<ul class="nut_table">
<li>
<div>SrNo</div>
<div>Name</div>
<div class="panel-delete-cc"> </div>
</li>
</ul>
<ul id="selected-ingre" class="selected-ingre nut_table_inner">
<li class="default-ingre-item" id="ingre-item">
<div class="ingre-item-counter"></div>
<div class="ingre-item-text"></div>
<div class="panel-delete-ingre"></div>
<input type="hidden" id="ingredients">
</li>
<?php
//Sidebar addition
if($ingredients_settings <> ''){
$ingre_xml = new DOMDocument();
$ingre_xml->loadXML($ingredients_settings);
$counter = 0;
foreach( $ingre_xml->documentElement->childNodes as $ingre_name ){
$counter++;
?>
<li class="ingre-item" id="ingre-item">
<div class="ingre-item-counter"><?php echo $counter;?></div>
<div class="ingre-item-text"><?php echo $ingre_name->nodeValue; ?></div>
<div class="panel-delete-ingre"></div>
<input type="hidden" name="ingredients[]" id="ingredients" value="<?php echo $ingre_name->nodeValue; ?>">
</li>
<?php }
}
?>
</ul>
</ul>
<div class="clear"></div>
<ul class="recipe_class">
<li class="panel-title">
<label> <?php _e('Add Nutrition', 'crunchpress'); ?> </label>
</li>
<li class="panel-input">
<input type="text" id="add-more-nutrition" value="type title here" rel="type title here">
<div id="add-more-nutrition" class="add-more-nutrition"></div>
</li>
<li class="description"><p>Add Nutrition for this recipe.</p></li>
<div class="clear"></div>
<ul class="nut_table">
<li>
<div>SrNo</div>
<div>Name</div>
<div class="panel-delete-cc"> </div>
</li>
</ul>
<br class="clear">
<ul id="selected-nutrition" class="selected-nutrition nut_table_inner">
<li class="default-nutrition-item" id="nutrition-item">
<div class="nut-item-counter"></div>
<div class="nutrition-item-text"></div>
<div class="panel-delete-nutrition"></div>
<input type="hidden" id="nutrition">
</li>
<?php
//Sidebar addition
if($nutrition_settings <> ''){
$ingre_xml = new DOMDocument();
$ingre_xml->loadXML($nutrition_settings);
$counter = 0;
foreach( $ingre_xml->documentElement->childNodes as $ingre_name ){
$counter++;
?>
<li class="nutrition-item" id="nutrition-item">
<div class="nut-item-counter"><?php echo $counter;?></div>
<div class="nutrition-item-text"><?php echo $ingre_name->nodeValue; ?></div>
<div class="panel-delete-nutrition"></div>
<input type="hidden" name="nutrition[]" id="nutrition" value="<?php echo $ingre_name->nodeValue; ?>">
</li>
<?php }
}
?>
</ul>
</ul>
<div class="clear"></div>
<ul class="recipe_class">
<li class="panel-title">
<label for="select_chef"><?php _e('SELECT CHEF', 'crunchpress'); ?></label>
</li>
<li class="panel-input">
<div class="combobox">
<select name="select_chef" id="select_chef">
<option>-- Select Chef --</option>
<?php
foreach(get_title_list_array('teams') as $our_team){?>
<option <?php if($select_chef == $our_team->post_name){echo 'selected';}?> value="<?php echo $our_team->post_name;?>"><?php echo $our_team->post_title?></option>
<?php }?>
</select>
</div>
</li>
<li class="description"><p>Please select Chef of this recipe.</p></li>
</ul>
<div class="clear"></div>
<input type="hidden" name="nutrition_type" value="nutrition">
<div class="clear"></div>
</div>
<div class="clear"></div>
<?php }
add_action('save_post','save_recipe_option_meta');
function save_recipe_option_meta($post_id){
$recipe_social = '';
$sidebars = '';
$right_sidebar_recpie = '';
$left_sidebar_recpie = '';
foreach($_REQUEST as $keys=>$values){
$$keys = $values;
}
if(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) return;
if(isset($nutrition_type) AND $nutrition_type == 'nutrition'){
$new_data = '<recipe_detail>';
$new_data = $new_data . create_xml_tag('recipe_price',$recipe_price);
$new_data = $new_data . create_xml_tag('recipe_url',$recipe_url);
$new_data = $new_data . create_xml_tag('recipe_social',$recipe_social);
$new_data = $new_data . create_xml_tag('sidebars',$sidebars);
$new_data = $new_data . create_xml_tag('right_sidebar_recipe',$right_sidebar_recipe);
$new_data = $new_data . create_xml_tag('left_sidebar_recipe',$left_sidebar_recipe);
$new_data = $new_data . '</recipe_detail>';
//Saving Sidebar and Social Sharing Settings as XML
$old_data = get_post_meta($post_id, 'recipe_detail_xml',true);
save_meta_data($post_id, $new_data, $old_data, 'recipe_detail_xml');
$recipe_setting_xml = '<recipe_ingredients>';
if(isset($_POST['ingredients'])){$ingredients = $_POST['ingredients'];
foreach($ingredients as $keys=>$values){
$recipe_setting_xml = $recipe_setting_xml . create_xml_tag('ingredients',$values);
}
}else{$ingredients = '';}
$recipe_setting_xml = $recipe_setting_xml . '</recipe_ingredients>';
//Saving Sidebar and Social Sharing Settings as XML
$old_data_ingre = get_post_meta($post_id, 'ingredients_settings',true);
save_meta_data($post_id, $recipe_setting_xml, $old_data_ingre, 'ingredients_settings');
$nutrition_setting_xml = '<recipe_nutrition>';
if(isset($_POST['nutrition'])){$nutrition = $_POST['nutrition'];
foreach($nutrition as $keys=>$values){
$nutrition_setting_xml = $nutrition_setting_xml . create_xml_tag('nutrition',$values);
}
}else{$nutrition = '';}
$nutrition_setting_xml = $nutrition_setting_xml . '</recipe_nutrition>';
//Saving Sidebar and Social Sharing Settings as XML
$old_data_nut = get_post_meta($post_id, 'nutrition_settings',true);
save_meta_data($post_id, $nutrition_setting_xml, $old_data_nut, 'nutrition_settings');
}
}
//FRONT END RECIPE LAYOUT
$recipe_div_size_num_class = array(
"Full-Image" => array("index"=>"1", "class"=>"sixteen ", "size"=>array(300,110), "size2"=>array(300,110), "size3"=>array(300,110)),
"Small-Thumbnail" => array("index"=>"2", "class"=>"sixteen", "size"=>array(445,175), "size2"=>array(445,175), "size3"=>array(445,175)));
// Print Recipe item
function print_recipe_item($item_xml){
wp_reset_query();
global $paged,$sidebar,$recipe_div_size_num_class,$post,$wp_query,$counter;?>
<!--<script src="<?php echo CP_PATH_URL;?>/frontend/js/jquery-filterable.js" type="text/javascript"></script>-->
<?php
if(empty($paged)){
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
}
//echo '<pre>';print_r($item_xml);die;
//print_r($event_div_size_num_class);
//$item_type = find_xml_value($item_xml, 'recipe-thumbnail-type');
$item_type = 'Full-Image';
// get the item class and size from array
$item_class = $recipe_div_size_num_class[$item_type]['class'];
$item_index = $recipe_div_size_num_class[$item_type]['index'];
$full_content = find_xml_value($item_xml, 'show-full-news-post');
if( $sidebar == "no-sidebar" ){
$item_size = $recipe_div_size_num_class[$item_type]['size'];
}else if ( $sidebar == "left-sidebar" || $sidebar == "right-sidebar" ){
$item_size = $recipe_div_size_num_class[$item_type]['size2'];
}else{
$item_size = $recipe_div_size_num_class[$item_type]['size3'];
}
// get the blog meta value
$header = find_xml_value($item_xml, 'header');
$num_fetch = find_xml_value($item_xml, 'num-fetch');
$num_excerpt = find_xml_value($item_xml, 'num-excerpt');
$category = find_xml_value($item_xml, 'category');
$show_filterable = find_xml_value($item_xml, 'show-filterable');
$category_name = '';
$category = ( $category == 'All' )? '': $category;
if( !empty($category) ){
$category_term = get_term_by( 'name', $category , 'recipe-category');
$category = $category_term->term_id;
$category_name = $category_term->name;
}
?>
<h2 class="heading"><?php echo $header;?></h2>
<?php
//Filterable Recipe Script start
if($show_filterable == 'Yes'){?>
<script>
jQuery(window).load(function() {
var filter_container = jQuery('#portfolio-item-holder<?php echo $counter?>');
filter_container.children().css('position','absolute');
filter_container.masonry({
singleMode: true,
itemSelector: '.portfolio-item:not(.hide)',
animate: true,
animationOptions:{ duration: 800, queue: false }
});
jQuery(window).resize(function(){
var temp_width = filter_container.children().filter(':first').width() + 20;
filter_container.masonry({
columnWidth: temp_width,
singleMode: true,
itemSelector: '.portfolio-item:not(.hide)',
animate: true,
animationOptions:{ duration: 800, queue: false }
});
});
jQuery('ul#portfolio-item-filter<?php echo $counter?> a').click(function(e){
jQuery(this).addClass("active");
jQuery(this).parents("li").siblings().children("a").removeClass("active");
e.preventDefault();
var select_filter = jQuery(this).attr('data-value');
if( select_filter == "All" || jQuery(this).parent().index() == 0 ){
filter_container.children().each(function(){
if( jQuery(this).hasClass('hide') ){
jQuery(this).removeClass('hide');
jQuery(this).fadeIn();
}
});
}else{
filter_container.children().not('.' + select_filter).each(function(){
if( !jQuery(this).hasClass('hide') ){
jQuery(this).addClass('hide');
jQuery(this).fadeOut();
}
});
filter_container.children('.' + select_filter).each(function(){
if( jQuery(this).hasClass('hide') ){
jQuery(this).removeClass('hide');
jQuery(this).fadeIn();
}
});
}
filter_container.masonry();
});
});
</script>
<ul id="portfolio-item-filter<?php echo $counter?>" class="category-list">
<li><a data-value="all" class="gdl-button active" href="#">All</a></li>
<?php
$categories = get_categories( array('child_of' => $category, 'taxonomy' => 'recipe-category', 'hide_empty' => 0) );
//$categories = get_the_terms( $post->ID, 'recipe-category' );
if($categories <> ""){
foreach($categories as $values){?>
<li><a data-value="<?php echo $values->term_id;?>" class="gdl-button" href="#"><?php echo $values->name;?></a></li>
<?php
}
}?>
<div class="clear"></div>
</ul>
<?php }else{?>
<h2 class="heading"><?php echo $category_name; ?></h2>
<?php }?>
<ul class="lightbox gallery_filterable" id="portfolio-item-holder<?php echo $counter?>">
<?php
$category = find_xml_value($item_xml, 'category');
$category = ( $category == 'All' )? '': $category;
if( !empty($category) ){
$category_term = get_term_by( 'name', $category , 'recipe-category');
$category = $category_term->slug;
}
if($show_filterable == 'Yes'){
query_posts(array(
'posts_per_page' => -1,
'post_type' => 'recipes',
'recipe-category' => $category,
'post_status' => 'publish',
'order' => 'ASC',
));
}else{
query_posts(array(
'posts_per_page' => $num_fetch,
'paged' => $paged,
'post_type' => 'recipes',
'recipe-category' => $category,
'post_status' => 'publish',
'order' => 'ASC',
));
}
while( have_posts() ){
global $post;
the_post(); ?>
<li class="all portfolio-item item alpha
<?php
$categories = get_the_terms( $post->ID, 'recipe-category' );
if($categories <> ''){
foreach ( $categories as $category ) {
echo $category->term_id." ";
}
}
?>">
<h3 class="heading">
<a href="<?php echo get_permalink();?>">
<?php
echo substr($post->post_title, 0, 20);
if ( strlen($post->post_title) > 20 ) echo "...";
?>
</a>
</h3>
<a href="<?php echo get_permalink();?>" class="caption">
<?php echo get_the_post_thumbnail($post->ID, $item_size);?>
<span class="hover-effect big zoom"></span>
</a>
<!--david-->
<article class="menu-det">
<p><?php echo strip_tags(mb_substr(get_the_content(),0,$num_excerpt));?> <a class="c-link" href="<?php echo $recipe_url;?>"><?php _e('Read More...', 'crunchpress'); ?></a></p>
</article>
</li>
<?php }//End While?>
</ul>
<div class="clear"></div>
<?php
if( find_xml_value($item_xml, "pagination") == "Yes" AND $show_filterable == 'No'){
pagination();
}
}
?>
Now, at the bottom of this page above, you will see a readmore link with the following content:
<article class="menu-det">
<p><?php echo strip_tags(mb_substr(get_the_content(),0,$num_excerpt));?> <a class="c-link" href="<?php echo $recipe_url;?>"><?php _e('Read M...', 'crunchpress'); ?></a></p>
</article>
Now the part giving me issues, is this one:
<?php echo $recipe_url;?>
So I basically only registered a new variable: $recipe_url;, and I replaced the original link href value of:
href="<?php echo get_permalink()?>"
with my new variable:
href="<?php echo $recipe_url;?>"
Now the url is empty on the webpage, even though a value is set. I even used some of the original variables included in the theme:
href="<?php echo $recipe_price;?>"
but it also does not populate a any url or href value..
Why is this? Am I calling it wrong?
Thanks
I don't have enough rep to add a comment to the question, but you don't appear to have added a closing brace to the add_recipe_option_element() method either.

Categories