how use enclosing shortcodes in php ( wordpress theme ) - php

edd restrict content plugin creates this shortcodes and these are working in pages and posts :
[edd_restrict id="any"]sample restricted html or text[/edd_restrict]
but i want to use it in my theme not in the posts or pages.
i tried this :
<?php =echo do_shortcode( '[edd_restrict id="any"]' . sample text . '[/edd_restrict]' );?>
but theme shows me fatal error.
so how can i use this shortcodes in wordress theme?
sample text here will be a php code that i want to restrict.
let me tell you more... i want to put the line below between those shortcodes in my single.php in wordpress :
<li><?php if(get_post_meta($post->ID, 'download',true)!= ""){echo "<a href='".get_post_meta($post->ID, 'download',true)."' > دانلود با لینک مستقیم</a> ";} else { echo ""; } ?>
<?php if(get_post_meta($post->ID, 'download32',true)!= ""){echo "<a href='".get_post_meta($post->ID, 'download32',true)."'>لینک مستقیم نسخه 32bit / </a> ";} else { echo ""; } ?>
<?php if(get_post_meta($post->ID, 'download64',true)!= ""){echo "<a href='".get_post_meta($post->ID, 'download64',true)."'>لینک مستقیم نسخه 64bit </a> ";} else { echo ""; } ?>
<?php if(get_post_meta($post->ID, 'downloadwin',true)!= ""){echo "<a href='".get_post_meta($post->ID, 'downloadwin',true)."'>دانلود نسخه ویندوز </a> ";} else { echo ""; } ?></li>

The method do_shortcode accepts a string of a shortcode and it's attributes/content. So for example
<?php
$string = "some value or <b>html</b>";
// or (?)
$string = include TEMPLATEPATH . "/post-ads.php";
// or (!)
ob_start();
include TEMPLATEPATH . "/post-ads.php";
$string = ob_get_clean();
// for a specific use case:
if (get_post_meta($post->ID, 'download', true) != "") {
$string = "<a href='" . get_post_meta($post->ID, 'download', true) . "' >download</a> ";
} else {
$string = "";
}
if (get_post_meta($post->ID, 'download32', true) != "") {
$string2 = "<a href='" . get_post_meta($post->ID, 'download32', true) . "'>download 32bit / </a> ";
} else {
$string2 = "";
}
// after you have string:
echo do_shortcode('[edd_restrict id="any"]' . $string . '[/edd_restrict]');

Related

wrapping Wordpress text content in div

I have an Wordpress site using bootstrap framework. I have content (written in the admin posts panel) which contains images and text. I am trying to wrap the text content in row and col-md-10 tags (and leaving the images alone)
It is brought into my single.php page
<?php the_content(); ?>
I have tried playing around with preg_replace to the effect of
<?php
$content = preg_replace('/<row>(.*?)<\/row>/', '', get_the_content());
$content = wpautop($content);
echo $content;
?>
Try this piece of code. You will have to put it in your functions.php. What it does is that it searches for all the images and then wraps the content in divs leaving images intact. I have tested it. And it is working fine even if you have no images.
function sr_wrap_content_in_div( $content )
{
$contents = explode("<img", $content);
foreach($contents as $content)
{
$before_tag = strstr($content, '/>', true);
$after_tag = strstr($content, '/>');
if( $before_tag == '' && $after_tag == '' )
{
echo '<div class="row"><div class="col-md-10">'; // change it later if you need to
echo $content;
echo '</div></div>';
} else if( $after_tag == '/> ' )
{
echo '<img';
echo $before_tag;
echo '/>';
} else
{
echo '<img';
echo $before_tag;
echo '/>';
echo '<div class="row"><div class="col-md-10">'; // change here too.
echo substr($after_tag, 2);
echo '</div></div>';
}
}
}
add_filter( 'the_content', 'sr_wrap_content_in_div' );

(Wordpress) How can i get the full content of a post with the html tags - unstripped

I'm using WordPress for my site with the qtranslate plugin and i'm trying to display language flags in each post.
Qtranslate inserts html tags to the content and title like
"!--:en-->"
for each language that i used in each post
So i need a conditional that checks which of these html tags are included in the content so i can print the specific flags
something like this:
function language_pick(){
$qt_dir = "http://localhost/MY-SITE/wp-content/plugins/qtranslate-xp/flags/";
$cr_url = "http://".$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$en_url = esc_html($cr_url."&lang=en");
$fr_url = esc_html($cr_url."&lang=fr");
$it_url = esc_html($cr_url."&lang=it");
$es_url = esc_html($cr_url."&lang=es");
$query = get_post(get_the_ID());
$content = apply_filters('the_content', $query->post_content);
if(get_permalink() != $cr_url) { echo '<a style="margin-left:15px;" href="'.$cr_url.'" /><img src="'.$qt_dir.'gr.png"></a>'; }
if (strpos($content, '<!--:en-->') === true) {
if(get_permalink() != $en_url) { echo '<a style="margin-left:15px;" href="'.$en_url.'" /><img src="'.$qt_dir.'gb.png"></a>'; } }
if(strpos($content,'<!--:fr-->') === true) {
if(get_permalink() != $fr_url) { echo '<a style="margin-left:15px;" href="'.$fr_url.'" /><img src="'.$qt_dir.'fr.png"></a>'; } }
if(strpos($content,'<!--:it-->') === true) {
if(get_permalink() != $it_url) { echo '<a style="margin-left:15px;" href="'.$it_url.'" /><img src="'.$qt_dir.'it.png"></a>'; } }
if(strpos($content,'<!--:es-->') === true) {
if(get_permalink() != $es_url) { echo '<a style="margin-left:15px;" href="'.$es_url.'" /><img src="'.$qt_dir.'es.png"></a>'; } }
}
Very simply add <?= apply_filters('the_content', $content); ?>
There are loads of references to this on Google.
EDIT
So in this case:
$query = get_post(get_the_ID());
$content = apply_filters('the_content', $query->post_content);
echo $content;
This simply helped in single post template to render HTML tags as you see in the editor/
$content = apply_filters('the_content', get_the_content());
echo $content ;

WordPress breadcrumbs in search results

In WordPress i'm currently using Yoast's SEO Plugin to display breadcrumbs for my pages and posts, which is working fine when visiting a specific page.
Here is the function i'm using to display the breadcrumbs inside of my WordPress templates:
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
For example when browsing to Team Members which is a child of About Us I get:
Home > About Us > Team Members
However i'd like to be able to display the same breadcrumbs (for the individual pages and posts) inside the search results loop.
So far what displays when searching for Members is:
Your Search Results:
Team Members
Home > Search > Members
Members Area
Home > Search > Members
But I don't want breadcrumbs for the Search Results page, I want them for the individual pages and posts that are displayed as a result of searching for a keyword.
For example Imagine I searched again for Members I would like displayed the below:
Your Search Results:
Team Members
Home > About Us > Team Members
Members Area
Home > Members Area
I'm not fussed if this is or isn't integrated with the SEO plugin, however thus far it's the best solution I found to display breadcrumbs in WordPress!
Also incase abody requires it, here is my search.php file: http://pastebin.com/0qjb2954
Try this. That's my own working snippet that shows breadcrumbs inside search loop.
/*Begin Loop */
<?php
echo '<div class="b-search_result_list__item_breadcrumbs breadcrumbs">';
$current_type = get_post_type();
if ($current_type == 'page') {
$parents = get_post_ancestors(get_the_ID());
if($parents){
for($i=count($parents)-1;$i>=0;$i--){
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.get_the_title($parents[$i]).'" href="'.get_permalink($parents[$i]).'">'.get_the_title($parents[$i]).'</a>';
echo '</span>';
}
}else{
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.get_bloginfo('name').'" href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>';
echo '</span>';
}
echo '<span typeof="v:Breadcrumb">';
echo '<span property="v:title">'.get_the_title().'</span>';
echo '</span>';
}else{
$current_obj = get_post_type_object($current_type);
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.get_bloginfo('name').'" href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>';
echo '</span>';
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.$current_obj->labels->name.'" href="'.get_post_type_archive_link( $current_type ).'">'.$current_obj->labels->name.'</a>';
echo '</span>';
$current_taxonomies = get_object_taxonomies($current_type);
if($current_taxonomies){
$current_terms = get_the_terms(get_the_ID(), $current_taxonomies[0]);
if($current_terms){
$current_term = array_shift($current_terms);
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.$current_term->name.'" href="'.get_term_link($current_term).'">'.$current_term->name.'</a>';
echo '</span>';
/*
var_dump($current_obj->labels->name); // Archive name
var_dump(get_post_type_archive_link( $current_type )); // Archive link
var_dump($current_term->name); // Term name
var_dump(get_term_link($current_term)); // Term link
var_dump(get_permalink()); // Post link
*/
}
}
echo '<span typeof="v:Breadcrumb">';
echo '<span property="v:title">'.get_the_title().'</span>';
echo '</span>';
}
echo '</div>';
?>
/*End Loop*/
try adding this line of code above the yoast breadcrumb function in your search.php file:
WPSEO_Breadcrumbs::$instance = NULL;
This would be line 22 I believe, and also make sure to use the Yoast breadcrumb function from your question, not the new breadcrumb() function that's there now.
Please let me know if this works!
Full explanation:
The Yoast plugin breadcrumbs functionality is build on the page load, based on the current page as the child. To make it load the right child and parents, you'd need to reset it before you run the function. There is no built-in reset function, however setting the static $instance to NULL should cause the plugin to re-generate its data based on the current global post object which is set while you're looping.
Building upon Yavor's answer I found a way. Been banging my head about it for hours. You can place the backup and restore otuside of the loop though. Here it is:
global $wp_query;
//backup
$old_singular_value = $wp_query->is_singular;
//change
$wp_query->is_singular = true;
//reset
WPSEO_Breadcrumbs::$instance = NULL;
//breadcrumbs
if (function_exists('yoast_breadcrumb')){
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
}
//restore
$wp_query->is_singular = $old_singular_value;
It fakes the query to make it singular so the newly-refreshed breadcrumbs thinks that this is not the search page but a single post or page or whatever you are displaying as your search results.
Using a plugin to generate breadcrumbs is not really necessary. Here's a simple PHP function you can add to your functions.php file:
function breadcrumbs() {
global $post;
echo "<ul id='breadcrumbs'>";
if (!is_home()) {
echo '<li>Home</li>';
if (is_category() || is_single()) {
echo "<li>" . the_category(' </li><li> ');
if (is_single()) {
echo "</li><li>" . the_title() . "</li>";
}
} elseif (is_page()) {
if($post->post_parent){
foreach ( get_post_ancestors( $post->ID ) as $ancestor ) {
echo '<li>' . get_the_title($ancestor) . '</li>' . get_the_title();
}
} else {
echo "<li>" . get_the_title() . "</li>";
}
}
} elseif (is_tag()) {
single_tag_title();
} elseif (is_day()) {
echo "<li>Archive for " . the_time('F jS, Y') . "</li>";
} elseif (is_month()) {
echo "<li>Archive for " . the_time('F, Y') . "</li>";
} elseif (is_year()) {
echo "<li>Archive for " . the_time('Y') . "</li>";
} elseif (is_author()) {
echo "<li>Author Archive</li>";
} elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {
echo "<li>Blog Archives</li>";
} elseif (is_search()) {
echo "<li>Search Results for" . the_search_query() . "</li>";
}
echo "</ul>";
}
along with some CSS to style it, customize as you desire
#breadcrumbs {
list-style:none;
margin:5px 0;
overflow:hidden;
}
#breadcrumbs li{
float:left;
}
#breadcrumbs li+li:before {
content: '| ';
padding:0 4px;
}
You can then implement those breadcrumbs on any page you like, including your searchpage.php file or whichever file you use to display search results with this call
<?php breadcrumbs(); ?>
The search pages have a conditional function that can be used. You could always apply that to loading the breadcrumbs. Here is an example:
if ( ! is_search() ) {
if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
}
}
It depends where you are loading the breadcrumbs as well, but this should typically work unless your theme is very unique.

PHP Dynamic Count & Limit Menu items

I want to modify this code which works pretty good but (or I don't know because I'm new with php) I can't limit the number of li's displayed for the main elements in the menu. The actual code will echo all elements it finds, I want to limit the times
<li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>
this line is echoed.. let's say to echo just the first 15 elements + a "MORE" button under which to display the rest of the elements as sub-menus.. (this is a 2 level horizontal menu). Can someone please help me? I really tried a lot but I'm not an expert in PHP..
Thanks!
<?php
require_once( '../../../inc/header.inc.php' );
require_once( DIRECTORY_PATH_INC . 'membership_levels.inc.php' );
require_once( DIRECTORY_PATH_ROOT . "templates/tmpl_{$tmpl}/scripts/TemplMenu.php" );
class SimpleMenu extends TemplMenu
{
function getCode()
{
$this->iElementsCntInLine = 100;
$this->getMenuInfo();
$this->genTopItems();
return $this->sCode;
}
function genTopItem($sText, $sLink, $sTarget, $sOnclick, $bActive, $iItemID, $isBold = false, $sPicture = '')
{
$sActiveStyle = ($bActive) ? ' id="tm_active"' : '';
if (!$bActive) {
$sAlt= $sOnclick ? ( ' alt="' . $sOnclick . '"' ) : '';
$sTarget = $sTarget ? ( ' target="_parent"' ) : '';
}
$sLink = (strpos($sLink, 'http://') === false && !strlen($sOnclick)) ? $this->sSiteUrl . $sLink : $sLink;
$sSubMenu = $this->getAllSubMenus($iItemID);
$sImgTabStyle = $sPictureRep = '';
if ($isBold && $sPicture != '') {
$sPicturePath = getTemplateIcon($sPicture);
$sPictureRep = "<img src='{$sPicturePath}' style='vertical-align:middle;width:16px;height:16px;' />";
$sText = ' ';
$sImgTabStyle = 'style="width:38px;"';
}
$sMainSubs = ($sSubMenu=='') ? '' : " {$sSubMenu} </a>";
$this->sCode .= "
<li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>
<div id='submenu'>
<ul>
<li>{$sMainSubs}</li>
</ul>
</div>
</li>
";
}
}
$objMenu = new SimpleMenu();
echo "<ul id='ddmenu'>";
echo $objMenu->getCode();
echo "</ul>";
?>
It's difficult to tell exactly where you want to do this in your code, but I figure you're looking for something like:
<?php
for ($i = 0; i < 15; i ++){
echo "<li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>"
}
echo "<li><a href='more' target='_parent'>More...</a>"
?>

How to properly build a navigation menu that highlights the current page

I've setup a menu for a fairly simple site based on icant.co.uk. It's fairly simple with maybe 5 pages. The small site is mainly a mysql browser for a few tables using MATE. Theres a common.php file that contains the header & footer HTML so thats where I put the code below.
The code below highlights the current page on the menu. Its ugly and I'm sure there has to be a better way to do it.
Any help is appreciated, thank you!
heres my code
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
if ($currentFile == "orders.php"){
echo '<li id="active">Orders</li>';
}
else{
echo '<li>Orders</li>';
}
if ($currentFile == "customers.php"){
echo '<li id="active">Customer List</li>';
}
else{
echo '<li>Customer List</li>';
}
if ($currentFile == "order_details.php"){
echo '<li id="active">Order Details</li>';
}
else{
echo '<li>Order Details</li>';
}
?>
UPDATE For those curious, below is the working code!
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
// easier to manage in case you want more pages later
$pages = array(
array("file" => "orders.php", "title" => "Orders"),
array("file" => "order_details.php", "title" => "Order Details"),
array("file" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
$activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
$currentAppend = ($page['file'] == $currentFile) ? ' id="current' : "";
$menuOutput .= '<li' . $activeAppend . '>'
. '' . $page['title'] .''
. '</li>';
}
$menuOutput .= '</ul>';
echo $menuOutput;
?>
What I normally do is something like (for all elements...):
<li class="<?php if (condition) echo 'selected'; ?>">content part, links, etc.</li>
Not sure if that's what you meant, but this way you'll get rid of this ugly if-else:
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
// easier to manage in case you want more pages later
$pages = array(
array("file" => "orders.php", "title" => "Orders"),
array("file" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
$activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
$menuOutput .= '<li' . $activeAppend . '>'
. '' . $page['title'] .''
. '</li>';
}
$menuOutput .= '</ul>';
echo $menuOutput;
A more concise way of doing it (if you have short tags enabled) would be:
<li class="<?= $test=="your_page_name" ? 'selected' : 'not_selected'?>">Link Name</li>
It performs the same function as the first answer, just more concisely.
Here's a snippet from a project of mine. It it old ugly code, and uses tables, but you can just as easily use the idea for divs and cleaner markup. The trick is to make the navigation use a different class if the current page matches it's url.
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_home.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_home.php'>Billing Home</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_schedules.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_schedules.php'>Billing Schedules</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_outstanding.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_outstanding.php'>Outstanding</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_list.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_list.php'>List All</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_history.php'>Billing History</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_statement_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_statement_history.php'>Statement History</a></td></tr>

Categories