Need help on a small wordpress/ WooCommerce code snippet
Budget: $30 – $250 USD
I created a code snippet to insert random woocommerce products between paragraphs on all blog posts.
Somehow there is a bug that creates 2 issues:
1. When the code snippet is active, youtube videos are not embedded anymore
2. The displayed woocommerce products are kinda messed up, check attached images
Looking for a solution.
sample: https://lynkhero.com/ugc/short-video-for-your-girlfriend/
code:
function addParagraphs($content) {
if (is_single() && !(is_product())){
$the_category = get_the_category()[0];
switch($the_category->name) {
case 'Facebook':
$custom_str = '[product_category category="facebook" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Threads':
$custom_str = '[product_category category="threads" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Instagram':
$custom_str = '[product_category category="instagram" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'LinkedIn':
$custom_str = '[product_category category="linkedin" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Soundcloud':
$custom_str = '[product_category category="soundcloud" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Spotify':
$custom_str = '[product_category category="spotify" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'TikTok':
$custom_str = '[product_category category="tiktok" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Twitter':
$custom_str = '[product_category category="twitter" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Twitch':
$custom_str = '[product_category category="twitch" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'YouTube':
$custom_str = '[product_category category="youtube" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
default:
$custom_str = '[products per_page="2" columns="2" orderby="rand" order="rand"]';
break;
}
// you can add as many as you want:
$additions = array(
"<p>$custom_str</p>",
'<p>[Sassy_Follow_Icons]</p>',
"<p>$custom_str</p>",
"<p>$custom_str</p>",
"<p>[publishpress_authors_box layout='ppma_boxes_476999']</p>",
);
$content = get_the_content();
$output = ''; // define variable to avoid PHP warnings
$parts = explode("</p>", $content);
$count = count($parts); // call count() only once, it's faster
for($i=0; $i<$count; $i++) {
$output .= $parts[$i] ?? '';
$output .= '</p>';
$output .= $additions[$i] ?? '';
// non-existent additions does not concatenate
}
return $output;
}
else {
return $content;
}
}
add_filter('the_content','addParagraphs');
Somehow there is a bug that creates 2 issues:
1. When the code snippet is active, youtube videos are not embedded anymore
2. The displayed woocommerce products are kinda messed up, check attached images
Looking for a solution.
sample: https://lynkhero.com/ugc/short-video-for-your-girlfriend/
code:
function addParagraphs($content) {
if (is_single() && !(is_product())){
$the_category = get_the_category()[0];
switch($the_category->name) {
case 'Facebook':
$custom_str = '[product_category category="facebook" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Threads':
$custom_str = '[product_category category="threads" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Instagram':
$custom_str = '[product_category category="instagram" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'LinkedIn':
$custom_str = '[product_category category="linkedin" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Soundcloud':
$custom_str = '[product_category category="soundcloud" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Spotify':
$custom_str = '[product_category category="spotify" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'TikTok':
$custom_str = '[product_category category="tiktok" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Twitter':
$custom_str = '[product_category category="twitter" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'Twitch':
$custom_str = '[product_category category="twitch" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
case 'YouTube':
$custom_str = '[product_category category="youtube" per_page="2" columns="2" orderby="rand" order="rand"]';
break;
default:
$custom_str = '[products per_page="2" columns="2" orderby="rand" order="rand"]';
break;
}
// you can add as many as you want:
$additions = array(
"<p>$custom_str</p>",
'<p>[Sassy_Follow_Icons]</p>',
"<p>$custom_str</p>",
"<p>$custom_str</p>",
"<p>[publishpress_authors_box layout='ppma_boxes_476999']</p>",
);
$content = get_the_content();
$output = ''; // define variable to avoid PHP warnings
$parts = explode("</p>", $content);
$count = count($parts); // call count() only once, it's faster
for($i=0; $i<$count; $i++) {
$output .= $parts[$i] ?? '';
$output .= '</p>';
$output .= $additions[$i] ?? '';
// non-existent additions does not concatenate
}
return $output;
}
else {
return $content;
}
}
add_filter('the_content','addParagraphs');