WordPress page shows shortcodes - wordpress

My WordPress page shows shortcodes like [vc_row][vc_column][vc_column_text] .
Help me identify the root cause.
I have migrated the wordpress sql files from one host to another.

You could modify this Utility Script Runner script to remove shortcodes that no longer exist but are in your page content. Definitely do this on staging before you do it on live. high likelyhood of wrecking up the place. This script has been sitting for close to a year and I honestly can't remember if any of it doesn't work as expected. test a lot on staging before running this on anything you really care about.
<?php if(!defined('ABSPATH')) { die(); }
/**
* Utility Name: Remove and Flatten Shortcodes
* Description: Strip Shortcodes from all posts, with several processing options
* Supports: input
* Version: 0.0.2
**/
if (!class_exists('StripShortcodes')) {
class StripShortcodes {
var $wrappers = array(),
$swaps = array();
public static function Instance() {
static $instance = null;
if ($instance === null) {
$instance = new self();
}
return $instance;
}
public function __construct() {
add_filter('wp_util_script', array($this, 'strip_shortcodes_run'), 10, 3);
add_filter('wp_util_input_html', array($this, 'strip_shortcodes_input_html'));
}
public function strip_shortcodes_input_html( $html ) {
global $shortcode_tags;
$post_types = get_post_types( array(
'exclude_from_search' => false
), 'names' );
$post_types = array_diff( $post_types, array( 'revision', 'attachment' ) );
?>
<p>
<strong>WARNING! DO NOT RUN THIS ON A LIVE SITE!</strong><br>
This utility may cause data loss. Even restoring a backup may wipe out any changes since the last run of this utility.
</p>
<label>
<input type="checkbox" name="restore" value="restore"/>
<span>Restore From Backup</span>
</label>
<label>
<span>Post Types to Process</span>
<select name="post_types[]" multiple="multiple">
<option value="none" selected="selected">none</option>
<option value="any">all</option>
<?php
foreach( $post_types as $post_type ) {
echo '<option value="' . esc_attr( $post_type ) . '">' . $post_type . '</option>';
}
?>
</select>
</label>
<hr/>
<p>
Select what you would like to do with each shortcode.
</p>
<?php
if( !empty( $shortcode_tags ) ) {
foreach( $shortcode_tags as $tag => $callable ) {
?>
<div class="shortcode-options-wrapper">
<label>
<span>[<?php echo $tag; ?>]</span>
<select class="shortcode-select" name="shortcodes[<?php echo esc_attr( $tag ); ?>]"/>
<option value="skip">Skip (do not process)</option>
<option value="strip">Remove (deletes shortcode content)</option>
<option value="unwrap">Unwrap Content</option>
<option value="parse">Flatten (parses to HTML)</option>
<option value="swap">Swap (Replace with another shortcode)</option>
</select>
</label>
</div>
<?php
}
}
echo $this->build_form_script();
return ob_get_clean();
}
private function build_form_script () {
global $shortcode_tags;
ob_start(); ?>
<script type="text/javascript">
(jQuery)(function($) {
'use strict';
var unwrap = '<div class="wrap-wrapper"><p>Wrapper for content (use "sprintf()" formatting, including the %s for the content)</p><label>Wrapper<input class="shortcode-wrap"></label></div>';
var swap = '<div class="swap-wrapper"><select class="shortcode-swap"><?php foreach ($shortcode_tags as $tag => $callable) { echo '<option value="' . $tag . '">' . esc_attr($tag) . '</option>'; }?></select></div>'
$(document).on('change', '.shortcode-select', function () {
var $this = $(this);
var name = $this.attr('name');
if ($this.val() == 'unwrap') {
$this.closest('.shortcode-options-wrapper').append(unwrap);
$this.closest('.shortcode-options-wrapper').find('.shortcode-wrap').attr('name', 'wrap_' + name);
$this.closest('.shortcode-options-wrapper').find('.swap-wrapper').remove();
}
else if ($this.val() == 'swap') {
$this.closest('.shortcode-options-wrapper').append(swap);
$this.closest('.shortcode-options-wrapper').find('.shortcode-swap').attr('name', 'swap_' + name);
$this.closest('.shortcode-options-wrapper').find('.wrap-wrapper').remove();
} else {
$this.closest('.shortcode-options-wrapper').find('.wrap-wrapper').remove();
$this.closest('.shortcode-options-wrapper').find('.swap-wrapper').remove();
}
})
});
</script>
<?php return ob_get_clean();
}
public function strip_shortcodes_run( $legacy, $state, $atts ) {
$batch = 10;
$offset = 0;
if( !empty( $state['offset'] ) ) {
$offset = $state['offset'];
}
$result = array(
'state' => 'error',
'message' => 'an unknown error has occurred',
);
$post_types = 'none';
if( !empty( $atts['post_types'] ) && !in_array( 'none', $atts['post_types'] ) ) {
$post_types = $atts['post_types'];
}
$shortcode_settings = array();
if( !empty( $atts['shortcodes'] ) ) {
$shortcode_settings['core'] = $atts['shortcodes'];
$shortcode_settings['wrap'] = $atts['wrap_shortcodes'];
$shortcode_settings['swap'] = $atts['swap_shortcodes'];
}
$restore = true;
if( empty( $atts['restore'] ) ) {
$this->replace_shortcode_functions( $shortcode_settings );
$restore = false;
}
$query = new WP_Query( array(
'posts_per_page' => $batch,
'offset' => $offset,
'post_type' => $post_types
) );
if( !$query->have_posts() ) {
$result = array(
'state' => 'complete',
'message' => 'successfully processed all posts'
);
} else {
$offset += $query->post_count;
while( $query->have_posts() ) {
$query->the_post();
$post = get_post();
$backup = get_post_meta( $post->ID, 'flatten_shortcodes_backup', true );
if( $restore ) {
if( $backup ) {
$post->post_content = $backup;
wp_update_post( $post );
delete_post_meta( $post->ID, 'flatten_shortcodes_backup' );
}
} else {
if( !$backup ) {
update_post_meta( $post->ID, 'flatten_shortcodes_backup', $post->post_content );
}
$post->post_content = do_shortcode( $post->post_content );
wp_update_post( $post );
}
}
$result = array(
'state' => array(
'offset' => $offset
),
'message' => $offset . ' posts processed'
);
}
return $result;
}
private function replace_shortcode_functions( $settings = array() ) {
global $shortcode_tags;
foreach( $shortcode_tags as $tag => $callable ) {
$setting = 'skip';
if( !empty( $settings['core'][$tag] ) ) {
$setting = $settings['core'][$tag];
}
switch( $setting ) {
case 'strip' :
$shortcode_tags[$tag] = "__return_empty_string";
break;
case 'unwrap':
$shortcode_tags[$tag] = array($this, 'replace_shortcode_unwrap');
$this->wrappers[$tag] = $settings['wrap'][$tag];
break;
case 'parse' :
// nothing needed
break;
case 'swap' :
$shortcode_tags[$tag] = array($this, 'swap_shortcode');
$this->swaps[$tag] = $settings['swap'][$tag];
break;
case 'skip' :
default :
unset( $shortcode_tags[$tag] );
}
}
}
public function replace_shortcode_unwrap( $atts=array(), $content='', $tag ) {
return sprintf($this->wrappers[$tag], $content);
}
public function swap_shortcode( $atts=array(), $content='', $tag ) {
$attString = '';
$newTag = $this->swaps[$tag];
if (!empty($atts)) {
foreach ($atts as $key => $att) {
$attString .= ' ' . $key . '="' . $att . '"';
}
}
$output = '[' . $newTag . $attString . ']';
if ($content) {
$output .= $content . '[/' . $newTag . ']';
}
return $output;
}
}
StripShortcodes::Instance();
}

I have got same result. My theme is wp baker. I activated edge core plugin and other required plug ins and it was solved.

Related

Place ads after second paragraph of the posts in a specific category

I want to place ads after second paragraph of the posts located under a specific category. For this I am trying to run the following piece of codes but it is not working. Kindly help me out.
//Insert ads after second paragraph of single post content.
$catarray = get_the_category( $post->ID );
foreach ($catarray as $cat) {
$catid = $cat->term_id;
if ($catid == 124);
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>Insert Ad code here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
you can use this code for it
add_filter('the_content', 'ak_the_content_filter', 10, 1);
function ak_the_content_filter($content)
{
$catarray = get_the_category( $post->ID );
foreach ($catarray as $cat) {
$catid = $cat->term_id;
if ($catid != 124) { return $content; }
else{
$parags = explode('</p>', $content);
$parags[1] .= '<br>'.do_shortcode('[SHORTCODE HERE]');// add whatever you want after first paragraph
$content_new = '';
foreach ($parags as $parag) {
$content_new .= $parag;
}
return $content_new;
}
}
}

can not add woocommerce order filter by vendors

woocommerce order page in, I want to filter orders by vendor. I am using the wcfm marketplace plugin.
Vendor name appears here. but when I filter, the orders are not listed.
please help :(woocommerce order page
function wcmp_admin_filter_by_vendor() {
global $typenow;
if ($typenow == 'shop_order') {
$admin_dd_html = '<select name="admin_order_vendor" id="dropdown_admin_order_vendor"><option value="">'.__("Show All Vendors", "dc-woocommerce-multi-vendor").'</option>';
$vendors = get_wcmp_vendors();
if($vendors) :
foreach ($vendors as $vendor) {
$admin_dd_html .= '<option value="'.$vendor->term_id.'">'.$vendor->page_title.'</option>';
}
endif;
$admin_dd_html .= '</select>';
echo $admin_dd_html;
}
}
add_action( 'restrict_manage_posts', 'wcmp_admin_filter_by_vendor');
function get_vendor_parent_order($id) {
$vendor_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_vendor_id',
'meta_value' => $id,
'post_type' => 'shop_order',
'post_status' => 'any',
) );
foreach( $vendor_orders as $vendor_order ) {
$parent_order = wp_get_post_parent_id( $vendor_order->ID );
$parent_orders[] = $parent_order;
}
return $parent_orders;
}
function filter_orders_by_vendor_in_admin_dashboard( $query ) {
if (current_user_can('administrator') && !empty($_REQUEST['admin_order_vendor'])) {
$vendor_term_id = isset($_GET['admin_order_vendor'])?$_GET['admin_order_vendor']:'';
$vendor = get_wcmp_vendor_by_term($vendor_term_id);
$parent_orders = get_vendor_parent_order($vendor->id);
$query['post__in'] = $parent_orders;
return $query;
}
return $query;
}
add_filter( 'wcmp_shop_order_query_request', 'filter_orders_by_vendor_in_admin_dashboard');
function remove_wcmp_order_hook() {
global $WCMp;
remove_action( 'manage_shop_order_posts_custom_column', array($WCMp->order, 'wcmp_show_shop_order_columns'), 99, 2 );
}
add_action('init', 'remove_wcmp_order_hook');
function wcmp_show_shop_order_columns($column, $post_id) {
global $WCMp;
switch ($column) {
case 'wcmp_suborder' :
$wcmp_suborders = $WCMp->order->get_suborders($post_id);
if ($wcmp_suborders) {
echo '<ul class="wcmp-order-vendor" style="margin:0px;">';
foreach ($wcmp_suborders as $suborder) {
$vendor = get_wcmp_vendor(get_post_field('post_author', $suborder->get_id()));
$vendor->ID = get_post_field('post_author', $suborder->get_id());
$vendor_term_id = isset($_GET['admin_order_vendor'])?$_GET['admin_order_vendor']:'';
$filter_vendor = get_wcmp_vendor_by_term($vendor_term_id);
$filter_vendor_id = isset($filter_vendor->id)?$filter_vendor->id:'';
if( $vendor->ID == $filter_vendor_id || $filter_vendor_id == '' ) {
$order_uri = apply_filters('wcmp_admin_vendor_shop_order_edit_url', esc_url('post.php?post=' . $suborder->get_id() . '&action=edit'), $suborder->get_id());
printf('<li><mark class="%s tips" data-tip="%s">%s</mark> <strong>#%s</strong> – <small class="wcmp-order-for-vendor">%s %s</small></li>', sanitize_title($suborder->get_status()), $suborder->get_status(), $suborder->get_status(), $order_uri, $suborder->get_order_number(), _x('for', 'Order table details', 'dc-woocommerce-multi-vendor'), $vendor->page_title
);
}
do_action('wcmp_after_suborder_details', $suborder);
}
echo '<ul>';
} else {
echo '<span class="na">–</span>';
}
break;
}
}
add_action('manage_shop_order_posts_custom_column', 'wcmp_show_shop_order_columns', 99, 2);

WooCommerce Rest-API Update Product

I'm trying to update remote woocommerce product but i'm stucked. How can we complete this code:
class myDemo {
public function __construct() {
$this->url = 'http://localhost/alfa/wp-json/wc/v2/products/2213';
$this->meta_key = 'whatever';
$this->consumer_key = 'ck_5978846499ea9bb1554b420e59072e46b8dedb1d';
$this->consumer_secret = 'cs_1009590c5443306f2082b2e508098be37fba2ee5';
$this->access_token = '';
$this->access_token_secret = '';
$this->method = 'PUT';
$this->set_signature_key();
$this->set_nonce();
$this->set_headers();
$this->set_base_string();
$this->set_signature();
$this->set_headers();
$this->set_header_string();
}
function get_response() {
$url = $this->url;
if( ! empty( $this->meta_key ) ) {
$url = add_query_arg( array( 'meta_key' => $this->meta_key ), $url );
}
$args = array(
'method' => $this->method,
'headers' => array(
'Authorization' => 'OAuth ' . $this->header_string,
),
);
$out = wp_remote_request( $url, $args );
return $out;
}
function set_signature_key() {
$this->signature_key = urlencode( $this->consumer_secret ) . '&' . urlencode( $this->access_token_secret );
}
function set_nonce() {
$this->nonce = wp_create_nonce( rand() . $this->url . $this->method );
}
function set_headers() {
if( ! isset( $this->headers ) ) {
$this->headers = array(
'oauth_consumer_key' => $this->consumer_key,
'oauth_nonce' => $this->nonce,
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_token' => $this->access_token,
'oauth_version' => '1.0',
);
} elseif( isset( $this->signature ) ) {
$this->headers['oauth_signature'] = $this->signature;
}
}
function set_base_string() {
$headers = $this->headers;
$url_params = array(
'meta_key' => $this->meta_key
);
$headers_and_params = array_merge( $headers, $url_params );
ksort( $headers_and_params );
$headers_and_params_string = '';
foreach( $headers_and_params as $key => $value ) {
$headers_and_params_string .= "$key=$value&";
}
$headers_and_params_string = rtrim( $headers_and_params_string, '&' );
$out = $this->method . '&' . rawurlencode( $this->url ) . '&' . rawurlencode( $headers_and_params_string );
$this->base_string = $out;
}
function set_signature() {
$out = hash_hmac( 'sha1', $this->base_string, $this->signature_key, TRUE );
$out = base64_encode( $out );
$this->signature = $out;
}
function set_header_string() {
// Will hold the combined string of headers.
$out = '';
foreach( $this->headers as $key => $value ) {
$value = rawurlencode( $value );
$out .= $key . '=' . '"' . $value . '"' . ', ';
}
$out = rtrim( $out, ', ' );
$this->header_string = $out;
}
}
$demo = new myDemo();
die(json_encode($demo->get_response()));
I'm interesting with that:
https://woocommerce.github.io/woocommerce-rest-api-docs/?php#update-a-product
I saw this code part:
$data = [
'regular_price' => '24.54'
];
I must send this data array to remote woocommerce. But i don't know how.
Thanks for everything.

WP shortcode output always at top of page

When placing a shortcode on a custom WordPress page the output is always displayed at the top of my page content.
I found out that the problem can be fixed by either using return instead of echo, or by using output buffering: (ob_start() / ob_get_contents())
Unfortunately my coding skills are not what I would like them to be.. And I don't know exactly where to implement these fixes.
Can someone help me out please? The plugin developer is not responding to my mails and I need to get this to work a.s.a.p.
I assume this needs to be implemented in the faulty plugin's functions file so I added it below.
<?php
/**
* Woocommerce Category Accordion Functions
*
* #author TechieResource
* #category Shortcode
* #package woocommerce-category-accordion/inc
* #version 1.2.1
*/
/**
/* Clean variables
*
* #param string $var
* #return string
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
class trwca_wc_category_accordion{
/**
* Constructor
*/
private $shortcode_tag = "WC-Category-Accordion";
public function __construct() {
add_action( 'woocommerce_category_accordion', array( $this, 'woocommerce_category_accordion_func' ), 10, 2 );
add_shortcode( $this->shortcode_tag, array( $this, 'wc_category_accordion_sc' ) );
if ( is_admin() ){
add_action('admin_head', array( $this, 'admin_head') );
add_action( 'admin_enqueue_scripts', array($this , 'admin_enqueue_scripts' ) );
}
}
/**
* Display the Woocommerce Category Accordion.
* #since 1.2.1
* #param array $instance Arguments.
* #param bool $echo Whether to display or return the output.
* #return string
*/
public function woocommerce_category_accordion_func( $instance, $echo = true ) {
extract( $instance, EXTR_SKIP );
global $wp_query;
global $post, $product;
$exclude_tree = esc_attr($exclude_tree );
$hide_empty = esc_attr($hide_empty );
$show_count = esc_attr($show_count );
$open_cat = esc_attr($open_cat );
$ac_speed = esc_attr($ac_speed );
$ac_type = esc_attr($ac_type );
$event_type = esc_attr($event_type );
$ac_icon = esc_attr($ac_icon );
$sortby = esc_attr($sortby );
$ac_theme = esc_attr($ac_theme );
$order = esc_attr($order );
$level = esc_attr($level );
$cats_id = esc_attr($ac_opencat);
$disable_parent = esc_attr($disable_parent);
$disable_aclink = esc_attr($disable_aclink);
if(!empty($instance['id'])){
$widgetid= $instance['id'];
}
else{
if($sh_id!=""){
$widgetid= "wc_category_accordion-".$sh_id;
}
else{
$widgetid= "wc_category_accordion-".$this->trwca_generate_random_code(3);
}
}
$instance_categories = get_terms( 'product_cat', '&parent=0&exclude='.$exclude_tree.'');
if (is_array($instance_categories)) {
foreach($instance_categories as $categories) {
$term_id[] = $categories->term_id;
$term_name = $categories->name;
}
}
if(!empty($post->ID)){
$terms =get_the_terms( $post->ID, 'product_cat' );
}
else {
$terms="";
}
if (is_array($terms )) {
foreach ( $terms as $term) {
$_cat=$term->term_id;
break;
}
}
/* For current category highlight */
if(is_product()){
$current_cat= array();
$cat = $wp_query->get_queried_object();
if (!empty($cat->term_id))
{
$current_cat = $cat->term_id;
}
else{
$_cat_id="1";
if (isset($term->term_id))
{
$_cat=$term->term_id;
$_cat_id = !empty($_cat) ? $_cat_id=$_cat : $_cat_id=1 ;
}
if (is_shop())
{
$_cat_id="1";
}
if (!is_shop()){
if (is_array($terms )) {
foreach($terms as $term){
$myterms[]= $term->term_id;
}
$cats_id=end($myterms);
?>
<script type="text/javascript">
var cats_id= <?php return end($myterms); ?>;
</script>
<style type="text/css">
<?php foreach((get_the_terms($post->ID, 'product_cat')) as $term) {
$myterms= $term->term_id;
return 'ul.'.$widgetid.' li.cat-item-'.$myterms.' > a{font-weight:bold;}';
}
}
}
?>
</style>
<?php
}
}
$cat = $wp_query->get_queried_object();
if (!empty($cat->term_id) && !is_product() ){
$cats_id = $cat->term_id;
return '<script type="text/javascript">
var cats_id= '.$cats_id.';
</script>';
}
else if(!is_product_category() && !is_product()){
$cats_id=$ac_opencat;
}
$ac_type = $ac_type=="toggle" ? $ac_type= "true" : $ac_type= "false";
$open_cat = $open_cat== true || $open_cat =='on' ? $open_cat=true : $open_cat=false;
/* Icon Selection */
switch ($ac_icon) {
case 'angle' :
$open_icon="angle-down";
$close_icon="angle-right";
break;
case 'doubleangle' :
$open_icon="angle-double-down";
$close_icon="angle-double-right";
break;
case 'arrow-circle1' :
$open_icon="arrow-circle-down";
$close_icon="arrow-circle-right";
break;
case 'arrow-circle2' :
$open_icon="arrow-circle-o-down";
$close_icon="arrow-circle-o-right";
break;
case 'arrow-right' :
$open_icon="arrow-down";
$close_icon="arrow-right";
break;
case 'caret' :
$open_icon="caret-down";
$close_icon="caret-right";
break;
case 'caret-square' :
$open_icon="caret-square-o-down";
$close_icon="caret-square-o-right";
break;
case 'chevron' :
$open_icon="chevron-down";
$close_icon="chevron-right";
break;
case 'chevron-circle' :
$open_icon="chevron-circle-down";
$close_icon="chevron-circle-right";
break;
case 'hand' :
$open_icon="hand-o-down";
$close_icon="hand-o-right";
break;
case 'plus' :
$open_icon="minus";
$close_icon="plus";
break;
case 'plus-circle' :
$open_icon="minus-circle";
$close_icon="plus-circle";
break;
case 'plus-square1' :
$open_icon="minus-square";
$close_icon="plus-square";
break;
case 'plus-square2' :
$open_icon="minus-square-o";
$close_icon="plus-square-o";
break;
}
if($disable_aclink==true){
$disable_aclink='true';
}
else if($disable_aclink==""){
$disable_aclink= 'false';
}
if($disable_parent==true){
$disable_parent='true';
}
else if($disable_parent==""){
$disable_parent='false';
}
$cats_id= empty($cats_id) ? 1 : $cats_id;
?>
<script type="text/javascript">
var $=jQuery.noConflict();
$(document).ready(function($){
$('.<?php echo $widgetid; ?>').trwcAccordion({
classParent : 'trwca-parent',
classActive : 'active',
classArrow : 'trwca-icon',
classCount : 'trwca-count',
classExpand : 'trwca-current-parent',
eventType : '<?php echo $event_type; ?>',
hoverDelay : 100,
menuClose : true,
cats_id: <?php echo $cats_id; ?>,
ac_type : <?php echo $ac_type; ?>,
autoExpand : true,
speed : '<?php echo $ac_speed ?>',
saveState : '<?php echo $open_cat; ?>',
disableLink : <?php echo $disable_aclink; ?>,
disableparentLink : <?php echo $disable_parent; ?>,
auto_open: 1,
showCount : true,
widget_id : "<?php echo $widgetid; ?>",
openIcon : '<?php echo $open_icon; ?>',
closeIcon : '<?php echo $close_icon; ?>',
});
});
</script>
<div class="block-content trwca-actheme <?php echo $ac_theme; ?>">
<div class="trwca-loader"></div>
<ul class="<?php echo $widgetid; ?> accordion" id="outer_ul">
<?php
$subcat_args = array(
'taxonomy' => 'product_cat',
'title_li' => '',
'orderby' => $sortby,
'order' => $order,
'depth' => $level,
'show_count' => $show_count,
'hide_empty' => $hide_empty,
'use_desc_for_title' => 1,
'echo' => false,
'exclude' => $exclude_tree,
'hierarchical' => true ,
'show_option_none' => __('No Categories Found','trwca'),
'link_after' => '',
'walker'=> new trwca_walker
);
?>
<?php $subcategories = wp_list_categories( $subcat_args );
$subcategories=preg_replace_callback(
'/<\/a> \(([0-9]+)\)/',
function ($matches) {
return ' <span class="count">('.($matches[1]).')</span></a>';
},$subcategories
);
?>
<?php if ( $subcategories ) {
echo $subcategories;
}
?>
</ul>
</div>
<?php
}
public function trwca_generate_random_code($length=3) {
$string = '';
$characters = "123456789";
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters)-1)];
}
return $string;
}
/**
* admin_head
* calls your functions into the correct filters
* #return void
*/
function admin_head() {
// check user permissions
if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
return;
}
// check if WYSIWYG is enabled
if ( 'true' == get_user_option( 'rich_editing' ) ) {
add_filter( 'mce_external_plugins', array( $this ,'mce_external_plugins' ) );
add_filter( 'mce_buttons', array($this, 'mce_buttons' ) );
}
}
/**
* mce_external_plugins
* Adds our tinymce plugin
* #param array $plugin_array
* #return array
*/
function mce_external_plugins( $plugin_array ) {
$plugin_array['WC_Category_Accordion'] = plugins_url( 'admin/js/mce-button.js' , __FILE__ );
return $plugin_array;
}
/**
* mce_buttons
* Adds our tinymce button
* #param array $buttons
* #return array
*/
function mce_buttons( $buttons ) {
array_push( $buttons, 'WC_Category_Accordion' );
return $buttons;
}
/**
* admin_enqueue_scripts
* Used to enqueue custom styles
* #return void
*/
function admin_enqueue_scripts(){
wp_enqueue_style('WC-Category-Accordion-sh', plugins_url( 'admin/css/mce-button.css' , __FILE__ ) );
}
public function wc_category_accordion_sc( $atts, $content = null ) {
$defaults = array(
'show_count' => 0,
'ac_speed' => 'fast',
'exclude_tree' =>'',
'hide_empty' => 0,
'sortby' =>'name',
'order' =>'ASC',
'level' => 0,
'event_type' => 'click',
'ac_type' => 'toggle',
'open_cat' => 0,
'ac_opencat' => 1,
'ac_icon' =>'plus',
'disable_parent' => 0,
'disable_aclink' => 0,
'ac_theme' => '',
'sh_id'=> '' );
$settings = shortcode_atts( $defaults, $atts );
return $this->woocommerce_category_accordion_func( $settings, false );
}
}
new trwca_wc_category_accordion();
function trwca_clean( $var ) {
return sanitize_text_field( $var );
}
}
?>
Thank you guys!
function your_shortcode_function(){
ob_start(); ?>
<div>
// all your stuffs here
</div>
<?php
$contents=ob_get_contents();
ob_end_clean();
return $contents;
}
add_shortcode('your_shortcode', 'your_shortcode_function');
You need ob_start and ob_end_clean() as said as above OUTPUT BUFFERING
You'll want to use ob_start() and return ob_get_clean() inside the shortcode function.
Likeso:
<?php
function my_shortcode(){ // The function that is our shortode output.
ob_start();?>
<div class="my-shortcode-output">
<em>I'm <strong>the best</strong></em>
</div>
<?php
return $ob_get_clean();
}
add_shortcode( 'my-shortcode', 'my_shortcode' );
I'd also consider not echoing js from your php, using wp_enque_script and wp_localize_script is much nicer.
This should work.
<?php
/**
* Woocommerce Category Accordion Functions
*
* #author TechieResource
* #category Shortcode
* #package woocommerce-category-accordion/inc
* #version 1.2.1
*/
/**
/* Clean variables
*
* #param string $var
* #return string
*/
if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
class trwca_wc_category_accordion
{
/**
* Constructor
*/
private $shortcode_tag = "WC-Category-Accordion";
public function __construct()
{
add_action('woocommerce_category_accordion', array(
$this,
'woocommerce_category_accordion_func'
), 10, 2);
add_shortcode($this->shortcode_tag, array(
$this,
'wc_category_accordion_sc'
));
if (is_admin()) {
add_action('admin_head', array(
$this,
'admin_head'
));
add_action('admin_enqueue_scripts', array(
$this,
'admin_enqueue_scripts'
));
}
}
/**
* Display the Woocommerce Category Accordion.
* #since 1.2.1
* #param array $instance Arguments.
* #param bool $echo Whether to display or return the output.
* #return string
*/
public function woocommerce_category_accordion_func($instance, $echo = true)
{
ob_start();
extract($instance, EXTR_SKIP);
global $wp_query;
global $post, $product;
$exclude_tree = esc_attr($exclude_tree);
$hide_empty = esc_attr($hide_empty);
$show_count = esc_attr($show_count);
$open_cat = esc_attr($open_cat);
$ac_speed = esc_attr($ac_speed);
$ac_type = esc_attr($ac_type);
$event_type = esc_attr($event_type);
$ac_icon = esc_attr($ac_icon);
$sortby = esc_attr($sortby);
$ac_theme = esc_attr($ac_theme);
$order = esc_attr($order);
$level = esc_attr($level);
$cats_id = esc_attr($ac_opencat);
$disable_parent = esc_attr($disable_parent);
$disable_aclink = esc_attr($disable_aclink);
if (!empty($instance['id'])) {
$widgetid = $instance['id'];
} else {
if ($sh_id != "") {
$widgetid = "wc_category_accordion-" . $sh_id;
} else {
$widgetid = "wc_category_accordion-" . $this->trwca_generate_random_code(3);
}
}
$instance_categories = get_terms('product_cat', '&parent=0&exclude=' . $exclude_tree . '');
if (is_array($instance_categories)) {
foreach ($instance_categories as $categories) {
$term_id[] = $categories->term_id;
$term_name = $categories->name;
}
}
if (!empty($post->ID)) {
$terms = get_the_terms($post->ID, 'product_cat');
} else {
$terms = "";
}
if (is_array($terms)) {
foreach ($terms as $term) {
$_cat = $term->term_id;
break;
}
}
/* For current category highlight */
if (is_product()) {
$current_cat = array();
$cat = $wp_query->get_queried_object();
if (!empty($cat->term_id)) {
$current_cat = $cat->term_id;
} else {
$_cat_id = "1";
if (isset($term->term_id)) {
$_cat = $term->term_id;
$_cat_id = !empty($_cat) ? $_cat_id = $_cat : $_cat_id = 1;
}
if (is_shop()) {
$_cat_id = "1";
}
if (!is_shop()) {
if (is_array($terms)) {
foreach ($terms as $term) {
$myterms[] = $term->term_id;
}
$cats_id = end($myterms);
?>
<script type="text/javascript">
var cats_id= <?php
return end($myterms);
?>;
</script>
<style type="text/css">
<?php
foreach ((get_the_terms($post->ID, 'product_cat')) as $term) {
$myterms = $term->term_id;
return 'ul.' . $widgetid . ' li.cat-item-' . $myterms . ' > a{font-weight:bold;}';
}
}
}
?>
</style>
<?php
}
}
$cat = $wp_query->get_queried_object();
if (!empty($cat->term_id) && !is_product()) {
$cats_id = $cat->term_id;
return '<script type="text/javascript">
var cats_id= ' . $cats_id . ';
</script>';
} else if (!is_product_category() && !is_product()) {
$cats_id = $ac_opencat;
}
$ac_type = $ac_type == "toggle" ? $ac_type = "true" : $ac_type = "false";
$open_cat = $open_cat == true || $open_cat == 'on' ? $open_cat = true : $open_cat = false;
/* Icon Selection */
switch ($ac_icon) {
case 'angle':
$open_icon = "angle-down";
$close_icon = "angle-right";
break;
case 'doubleangle':
$open_icon = "angle-double-down";
$close_icon = "angle-double-right";
break;
case 'arrow-circle1':
$open_icon = "arrow-circle-down";
$close_icon = "arrow-circle-right";
break;
case 'arrow-circle2':
$open_icon = "arrow-circle-o-down";
$close_icon = "arrow-circle-o-right";
break;
case 'arrow-right':
$open_icon = "arrow-down";
$close_icon = "arrow-right";
break;
case 'caret':
$open_icon = "caret-down";
$close_icon = "caret-right";
break;
case 'caret-square':
$open_icon = "caret-square-o-down";
$close_icon = "caret-square-o-right";
break;
case 'chevron':
$open_icon = "chevron-down";
$close_icon = "chevron-right";
break;
case 'chevron-circle':
$open_icon = "chevron-circle-down";
$close_icon = "chevron-circle-right";
break;
case 'hand':
$open_icon = "hand-o-down";
$close_icon = "hand-o-right";
break;
case 'plus':
$open_icon = "minus";
$close_icon = "plus";
break;
case 'plus-circle':
$open_icon = "minus-circle";
$close_icon = "plus-circle";
break;
case 'plus-square1':
$open_icon = "minus-square";
$close_icon = "plus-square";
break;
case 'plus-square2':
$open_icon = "minus-square-o";
$close_icon = "plus-square-o";
break;
}
if ($disable_aclink == true) {
$disable_aclink = 'true';
} else if ($disable_aclink == "") {
$disable_aclink = 'false';
}
if ($disable_parent == true) {
$disable_parent = 'true';
} else if ($disable_parent == "") {
$disable_parent = 'false';
}
$cats_id = empty($cats_id) ? 1 : $cats_id;
?>
<script type="text/javascript">
var $=jQuery.noConflict();
$(document).ready(function($){
$('.<?php
echo $widgetid;
?>').trwcAccordion({
classParent : 'trwca-parent',
classActive : 'active',
classArrow : 'trwca-icon',
classCount : 'trwca-count',
classExpand : 'trwca-current-parent',
eventType : '<?php
echo $event_type;
?>',
hoverDelay : 100,
menuClose : true,
cats_id: <?php
echo $cats_id;
?>,
ac_type : <?php
echo $ac_type;
?>,
autoExpand : true,
speed : '<?php
echo $ac_speed;
?>',
saveState : '<?php
echo $open_cat;
?>',
disableLink : <?php
echo $disable_aclink;
?>,
disableparentLink : <?php
echo $disable_parent;
?>,
auto_open: 1,
showCount : true,
widget_id : "<?php
echo $widgetid;
?>",
openIcon : '<?php
echo $open_icon;
?>',
closeIcon : '<?php
echo $close_icon;
?>',
});
});
</script>
<div class="block-content trwca-actheme <?php
echo $ac_theme;
?>">
<div class="trwca-loader"></div>
<ul class="<?php
echo $widgetid;
?> accordion" id="outer_ul">
<?php
$subcat_args = array(
'taxonomy' => 'product_cat',
'title_li' => '',
'orderby' => $sortby,
'order' => $order,
'depth' => $level,
'show_count' => $show_count,
'hide_empty' => $hide_empty,
'use_desc_for_title' => 1,
'echo' => false,
'exclude' => $exclude_tree,
'hierarchical' => true,
'show_option_none' => __('No Categories Found', 'trwca'),
'link_after' => '',
'walker' => new trwca_walker
);
$subcategories = wp_list_categories($subcat_args);
$subcategories = preg_replace_callback('/<\/a> \(([0-9]+)\)/', function($matches)
{
return ' <span class="count">(' . ($matches[1]) . ')</span></a>';
}, $subcategories);
if ($subcategories) {
echo $subcategories;
}
?>
</ul>
</div>
<?php
$contents=ob_get_contents();
ob_end_clean();
return $contents;
}
public function trwca_generate_random_code($length = 3)
{
$string = '';
$characters = "123456789";
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters) - 1)];
}
return $string;
}
/**
* admin_head
* calls your functions into the correct filters
* #return void
*/
function admin_head()
{
// check user permissions
if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) {
return;
}
// check if WYSIWYG is enabled
if ('true' == get_user_option('rich_editing')) {
add_filter('mce_external_plugins', array(
$this,
'mce_external_plugins'
));
add_filter('mce_buttons', array(
$this,
'mce_buttons'
));
}
}
/**
* mce_external_plugins
* Adds our tinymce plugin
* #param array $plugin_array
* #return array
*/
function mce_external_plugins($plugin_array)
{
$plugin_array['WC_Category_Accordion'] = plugins_url('admin/js/mce-button.js', __FILE__);
return $plugin_array;
}
/**
* mce_buttons
* Adds our tinymce button
* #param array $buttons
* #return array
*/
function mce_buttons($buttons)
{
array_push($buttons, 'WC_Category_Accordion');
return $buttons;
}
/**
* admin_enqueue_scripts
* Used to enqueue custom styles
* #return void
*/
function admin_enqueue_scripts()
{
wp_enqueue_style('WC-Category-Accordion-sh', plugins_url('admin/css/mce-button.css', __FILE__));
}
public function wc_category_accordion_sc($atts, $content = null)
{
$defaults = array(
'show_count' => 0,
'ac_speed' => 'fast',
'exclude_tree' => '',
'hide_empty' => 0,
'sortby' => 'name',
'order' => 'ASC',
'level' => 0,
'event_type' => 'click',
'ac_type' => 'toggle',
'open_cat' => 0,
'ac_opencat' => 1,
'ac_icon' => 'plus',
'disable_parent' => 0,
'disable_aclink' => 0,
'ac_theme' => '',
'sh_id' => ''
);
$settings = shortcode_atts($defaults, $atts);
return $this->woocommerce_category_accordion_func($settings, false);
}
}
new trwca_wc_category_accordion();
function trwca_clean($var)
{
return sanitize_text_field($var);
}
}
?>

Permalink structure for my custom_post_type

I have a “gallery” as custom_post_type and “albums” as taxonomry_name
How can i achieve this structure :
mydomain.com/gallery/albums/{taxonomy_term}/{post}
I've tried something like the example below but it didn't work or perhaps i haven't used it properly
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
$newRules = array();
$newRules['basename/(.+)/(.+)/(.+)/?$'] = 'index.php?gallery=$matches[3]'; // my custom structure will always have the post name as the 4th uri segment
$newRules['basename/(.+)/?$'] = 'index.php?albums=$matches[1]';
return array_merge($newRules, $rules);
}
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'gallery')
return $link;
if ($cats = get_the_terms($post->ID, 'albums'))
{
$link = str_replace('%albums%', get_taxonomy_parents(array_pop($cats)->term_id, 'albums', false, '/', true), $link); // see custom function defined below
}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {
$chain = '';
$parent = &get_term($id, $taxonomy);
if (is_wp_error($parent)) {
return $parent;
}
if ($nicename)
$name = $parent -> slug;
else
$name = $parent -> name;
if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {
$visited[] = $parent -> parent;
$chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);
}
if ($link) {
// nothing, can't get this working :(
} else
$chain .= $name . $separator;
return $chain;
}
I solved my problem by using the code below:
// Add our custom permastructures for custom taxonomy and post
add_action( 'wp_loaded', 'add_album_permastructure' );
function add_album_permastructure() {
global $wp_rewrite;
add_permastruct( 'albums', 'gallery/%albums%', false );
add_permastruct( 'gallery', 'gallery/%albums%/%gallery%', false );
}
// Make sure that all links on the site, include the related texonomy terms
add_filter( 'post_type_link', 'gallery_permalinks', 10, 2 );
function gallery_permalinks( $permalink, $post ) {
if ( $post->post_type !== 'gallery' )
return $permalink;
$terms = get_the_terms( $post->ID, 'albums' );
if ( ! $terms )
return str_replace( '%albums%/', '', $permalink );
$post_terms = array();
foreach ( $terms as $term )
$post_terms[] = $term->slug;
return str_replace( '%albums%', implode( ',', $post_terms ) , $permalink );
}
// Make sure that all term links include their parents in the permalinks
add_filter( 'term_link', 'add_term_parents_to_permalinks', 10, 2 );
function add_term_parents_to_permalinks( $permalink, $term ) {
$term_parents = get_term_parents( $term );
foreach ( $term_parents as $term_parent )
$permlink = str_replace( $term->slug, $term_parent->slug . ',' . $term->slug, $permalink );
return $permlink;
}
// Helper function to get all parents of a term
function get_term_parents( $term, &$parents = array() ) {
$parent = get_term( $term->parent, $term->taxonomy );
if ( is_wp_error( $parent ) )
return $parents;
$parents[] = $parent;
if ( $parent->parent )
get_term_parents( $parent, $parents );
return $parents;
}
Added this to fucntions.php and then refreshed my permalink structure from the Settings -> Permalink

Resources