Check requested url for a text pattern then do something - php

I am using the code below to execute video shortcode on my WordPress website but some pages already contain manually added video which will cause duplicate when i use the code.
How can include a check if the page already contain a YouTube embedded iframe or video link and exclude pages which already have videos, here is what i have below:
if (is_single() && in_category(1) )
{
echo '<h4 class="post-title entry-title">Video</h4>' ;
echo do_shortcode( '[yotuwp type="keyword" id="'.get_post_field( 'post_title', $post_id, 'raw' ).'" player="mode=large" template="mix" column="1" per_page="1"]' );
}
I want to include youtube link check here:
if (is_single() && in_category(1)
Here is what i am able to find Here but this scans the requested url instead of the content on it:
<?php
if (stripos($_SERVER['REQUEST_URI'],'tout') == true && stripos($_SERVER['REQUEST_URI'],'dedans') == true)
{echo '<div class="clear"></div> >> View all Cakes';}
?>

Since you already have the $post_id I suggest you get the Post object and do a regular expression match for 'youtube' or the short URL version 'youtu.be'. See sample code:
$post = get_post($post_id);
$content = apply_filters('the_content', $post->post_content);
if (is_single() && in_category(1) && !preg_match('/youtu\.?be/', $content)) {
echo '<h4 class="post-title entry-title">Video</h4>';
echo do_shortcode('[yotuwp type="keyword" id="' . get_post_field('post_title', $post_id, 'raw') . '" player="mode=large" template="mix" column="1" per_page="1"]');
}

Use file_get_contents, like this:
if (strpos(file_get_contents($_SERVER['REQUEST_URI']), 'youtube') === false) {
// you can add your video
}

I would recommand you to hook on the_content filter to check the current content if already has the wanted shortcode, if not, add it.
add_filter( 'the_content', 'func_53829055', 10 );
/**
* Check if the shortcode exists, if not, add it
*
* #param string $post_content
*
* #return string
*/
function func_53829055( $post_content ) {
// Add your custom conditions
if( ! is_single() && ! in_category( 1 ) ) {
return $post_content;
}
// Use a regex or strpos depending on the needs and post_content length/complexity
if ( false !== strpos( $post_content, '[yotuwp' ) ) {
// It already has the wanted shortcode, then return the post content
return $post_content;
}
// If add the video before post content
$before = '<h4 class="post-title entry-title">Video</h4>';
$before .= do_shortcode( 'my_shortcode' );
$before .= '<br />';
$post_content = $before . $post_content;
// If add the video after post content
$post_content .= '<br />';
$post_content .= '<h4 class="post-title entry-title">Video</h4>';
$post_content .= do_shortcode( 'my_shortcode' );
return $post_content;
}
Note that the priority has to be lower than 11, because shortcodes are replaced on 11 : add_filter( 'the_content', 'do_shortcode', 11 );

Related

How can I make tag page title dynamic

I have problem with my PHP code and I really need your help!
I want to make my tag page title dynamically and do these two steps:
1 : Get the number of posts inside this tag
2 : show the number (posts is this tag) before tag page title
I already get the number of posts but I cannot hook it into page title
this is the way how I get the number of posts :
add_filter( ‘the_title’, ‘mbpt_strip_tags_for_title’ );
function mbpt_strip_tags_for_title($title) {
if ( is_tag() ) {
$tag_id = get_queried_object()->term_id;
$term = get_tag( $tag_id );
$tags_post_count = $term->count;
$title = $tags_post_count . $title;
} else {
$title = $title;
}
return $title;
}
the_title is the post title, the title of the Tag Archive would be the_archive_title
add_filter( 'get_the_archive_title', 'mbpt_strip_tags_for_title' );
function mbpt_strip_tags_for_title($title) {
$term = get_queried_object();
if ( is_a( $term, 'WP_Term' ) && $term->taxonomy === 'post_tag' ) {
return $term->count . ' ' . $term->name;
}
return $title;
}

Insert custom field value before content

I have a custom meta field that I would like to insert in the_content automatically so that my AMP plugin can render the custom field value in the same way as the_content.
Currently I am using this code to display it:
<?php $video_value = get_post_meta( get_the_ID(), '_video', true ); ?>
<?php if ( ! empty( $video_value ) ) {?>
<div class="video-container"><?php echo $video_value; ?></div>
<?php } else { ?>
<?php the_post_thumbnail(); ?>
<?php } ?>
But I would like the $video_value to be inserted automatically before the_content.
You can use the the_content filter to do this. You can read more about it on the WordPress developer site.
But the code could be looking something like this:
function my_custom_content_filter($content){
global $post;
$video_value = get_post_meta($post->ID, '_video', true);
if($video_value){
return '<div class="video-container">' . $video_value . '</div>' . $content;
}else{
return get_the_post_thumbnail($post->ID) . $content;
}
}
add_filter('the_content', 'my_custom_content_filter');
And you can add this code in you functions.php file.
Note This filter only works on the_content() and not get_the_content()
You can do something like this -
and add the conditions that you want -
You might need to access Global $post to get the meta value
function custom_weird_name_before_after($content) {
if(is_page() || is_single() || $yourOwnConditions == true) {
$beforecontent = 'This line will go before the content - populate with whatever.';
$aftercontent = 'This will come after the content - makes sense right?';
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'custom_weird_name_before_after');
You can add this in functions.php

(Wordpress) How to add a simple text to the contents of a specific category (php)

the following code in my function.php can almost do the job nice and clean to all single pages. the problem is I want it to be filtered for a specific category ID:
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
if ( $GLOBALS['post']->ID == get_the_ID() ) {
$content .= 'Your new content here';
}
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
You don't need to use get_the_category and examine the results - Wordpress already has a function to check if your post is in a category: has_category. The advantage of this over get_the_category is that
you don't need the code to loop through all results to comparing the id (this is the part that is wrong in the other answer - it will only work if your post has one single category)
You can use not just the ID, but also the slug or name
You can check for multiple categories
The code you need is very simple, you just need to change one line!
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
/* pass an array with the IDs/names/slugs of the categories to check for, e.g. */
if ( has_category( array(12) ) )
$content .= 'Your new content here';
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
UPDATE:
Even though it wasn't in your question, if you actually want to include the content on all posts as well, you just need to do this:
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
$content .= 'Your new content here';
if ( has_category( array(12) ) )
$content .= '<br>Your new content here';
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
Modified your code idealy this condition is never making any sense because it always getting the id. $GLOBALS['post']->ID == get_the_ID()
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
if ( $GLOBALS['post']->ID == get_the_ID() ) {
$content .= 'Your new content here';
}
//getting the current post
global $post;
$category_detail=get_the_category( $post->ID );
if(!empty($category_detail))
{
$catId = $category_detail[0]->term_id;
// instead of 2 put your category id
if($catId == 2 && $catId != 0 )
{
$content .= 'Your new content here';
}
}
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
Note : above code is for only default post not for custom taxonomy for custom taxonomy

Return ACF field as a Shortcode using Wordpress Functions.php file

How do I use an Advanced Custom Field as a Shortcode. Ive used the following code in the Wordpress functions.php file but no luck.
Here is my Code:
function location_date_func( $atts ){
return "<?php the_field('location_date', 658); ?>";
}
add_shortcode( 'location_date', 'location_date_func' );
You need to register the shortcode properly, and make it return the data to display, not return a string with php code in it:
function location_date_func( $atts ){
//return string, dont echo it, so use get_field, not the_field
return get_field('location_date', 658);
}
//create function to register shortcode
function register_shortcodes(){
add_shortcode( 'location_date', 'location_date_func' );
}
// hook register function into wordpress init
add_action( 'init', 'register_shortcodes');
Or if you are using php 5.3+, you can use anonomous functions to acheive the same result:
add_action('init', function(){
add_shortcode('location_date', function(){
return get_field('location_date', 658);
});
});
Got it to work!
function location_date_func( $atts ){
return apply_filters( 'the_content', get_post_field( 'location_details', 658 ) );
}
add_shortcode( 'location_date_sc', 'location_date_func' );
If you want to return the value of an ACF field using the_field(), there is already a built in shortcode to do that.
[acf field="location_date" post_id="658"]
If you would like to reproduce it using the [location_date] shortcode, you need to use get_field() to return rather than echo the value. Syntax-wise, the only problem with your code is that you do not need the double quotes or <?php tags, since it should already be inside a PHP block. It will be functionally the same as the [acf] shortcode, but does not accept the post_id argument. This example will be hard coded to post ID 658 unless you modify it to accept an ID as part of the $atts or use the global $post;
function location_date_func( $atts ){
return get_field( 'location_date', 658 );
}
add_shortcode( 'location_date', 'location_date_func' );
add_shortcode('location_start_your_application_group', 'start_your_application_group');
function start_your_application_group() {
$start_your_application_group = '';
$start_your_application_group .= '<section class="start-your-application">';
if ( have_rows( 'start_your_application_group', 'option' ) ) :
while ( have_rows( 'start_your_application_group', 'option' ) ) : the_row();
$heading = get_sub_field( 'heading' );
$content = get_sub_field( 'content' );
if ( $heading !== '' ) {
$start_your_application_group .= '<h3 class="start-your-application__heading">' . $heading . '</h3>';
}
if ( $content !== '' ) {
$start_your_application_group .= '<div class="start-your-application__content">' . $content . '</div>';
}
$image = get_sub_field( 'image' );
if ( $image ) {
$start_your_application_group .= '<div class="start-your-application__image-container"><img class="start-your-application__image" src="' . $image['url'] .'" alt="' . $image['alt'] . '" /></div>';
}
endwhile;
endif;
$start_your_application_group .= '</section>';
return $start_your_application_group;
}

Custom Field URL Option for Woocommerce Product Thumbnail

Currently I'm using the Woocommerce plugin for affiliate products. I would like to be able to click on the thumbnail on the main page and go directly to amazon, for example. Currently it's setup so that once clicked it goes to the product detail page on my site. From there you can get to the amazon page. However, fewer clicks the better.
So I found the hook in the content-product.php page. What I did was wrap the whole thing in a URL and used a custom field to add in the URL. Doesn't work as intended. What happens is that the URL goes to amazon only when using one of the sale flash options. When turned off, the URL does not go to amazon, but to the product page on my site. I don't know where else to place the URL wrapper.
So i tried looking for the <a href="<?php the_permalink(); ?>"> that is currently controlling where the thumbnail goes. I traced the function to the woocommerce-template.php file. That's where I hit a dead end. I'm not sure where it is for the thumbnail currently.
Here is my modified code that works partially in the content-product.php page:
<div class="thumbnail-wrapper">
<a href="<?php echo get_post_meta( $post->ID, 'URLThumb', true ); ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
?>
</a>
</div>
Here is the thumbnail function that I can't seem to drill down further to find the existing <a href="<?php the_permalink(); ?>"> to change. This is on the woocommerce-template.php page.
if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
/**
* Get the product thumbnail for the loop.
*
* #access public
* #subpackage Loop
* #return void
*/
function woocommerce_template_loop_product_thumbnail() {
echo woocommerce_get_product_thumbnail();
}
}
File Name: woocommerce.php
File Location: wp-content/themes/'your-theme'/theme/woocommerce.php
Solution: Target external products via query of product type, looping in $product_url when external, and looping in get_permalink() when simple/variable. This code also accounts for opening external products in a new tab.
I'm going to post one version of what the code looked like before, and then another with my additions + modifications. In my theme, the first line of code I pasted exists on line 374 within woocommerce.php (this will differ depending on your theme, and some themes may not have a modified woocommerce.php file. If that is the case, just drag woocommerce.php into your theme directory from the plugin.
Code Before Addition/Modification:
function woocommerce_template_loop_product_thumbnail() {
global $product, $woocommerce_loop;
$i = 0;
$attachments = array();
$attachments[] = get_post_thumbnail_id();
$attachments = array_merge( $attachments, $product->get_gallery_attachment_ids() );
$original_size = wc_get_image_size( 'shop_catalog' );
if ( $woocommerce_loop['view'] == 'masonry_item' ) {
$size = $original_size;
$size['height'] = 0;
YIT_Registry::get_instance()->image->set_size('shop_catalog', $size );
}
switch ( $woocommerce_loop['products_layout'] ) {
case 'zoom':
if( isset( $attachments[1] ) ) {
echo '' . woocommerce_get_product_thumbnail() . '';
echo '<div class="attachments-thumbnail">';
while( $i < 3 ){
if( ! isset( $attachments[ $i ] ) ) break;
$src = wp_get_attachment_image_src( $attachments[ $i ], 'shop_catalog' );
$active = ( $i == 0 ) ? 'active' : '';
echo '<div class="single-attachment-thumbnail ' . $active . '" data-img="' . $src[0] . '">';
yit_image( "id=$attachments[$i]&size=shop_thumbnail&class=image-hover" );
echo '</div>';
$i++;
}
echo '</div>';
}
else {
echo '' . woocommerce_get_product_thumbnail() . '';
}
break;
case 'flip':
if( isset( $attachments[1] ) ) {
echo '<span class="face">' . woocommerce_get_product_thumbnail() . '</span>';
echo '<span class="face back">';
yit_image( "id=$attachments[1]&size=shop_catalog&class=image-hover" );
echo '</span></a>';
}
else {
echo '<span class="face">' . woocommerce_get_product_thumbnail() . '</span>';
}
break;
}
Code After Addition/Modification:
function woocommerce_template_loop_product_thumbnail() {
global $product, $woocommerce_loop;
if(!is_single() ) {
if( $product->is_type( 'external' ) ){
$product_url = $product->get_product_url() . '"target="_blank""';
} else( $producenter code heret_url = get_permalink());
} else ($product_url = get_permalink());
$i = 0;
$attachments = array();
$attachments[] = get_post_thumbnail_id();
$attachments = array_merge( $attachments, $product->get_gallery_attachment_ids() );
$original_size = wc_get_image_size( 'shop_catalog' );
if ( $woocommerce_loop['view'] == 'masonry_item' ) {
$size = $original_size;
$size['height'] = 0;
YIT_Registry::get_instance()->image->set_size('shop_catalog', $size );
}
switch ( $woocommerce_loop['products_layout'] ) {
case 'zoom':
if( isset( $attachments[1] ) ) {
echo '' . woocommerce_get_product_thumbnail() . '';
echo '<div class="attachments-thumbnail">';
while( $i < 3 ){
if( ! isset( $attachments[ $i ] ) ) break;
$src = wp_get_attachment_image_src( $attachments[ $i ], 'shop_catalog' );
$active = ( $i == 0 ) ? 'active' : '';
echo '<div class="single-attachment-thumbnail ' . $active . '" data-img="' . $src[0] . '">';
yit_image( "id=$attachments[$i]&size=shop_thumbnail&class=image-hover" );
echo '</div>';
$i++;
}
echo '</div>';
}
else {
echo '' . woocommerce_get_product_thumbnail() . '';
}
break;
case 'flip':
if( isset( $attachments[1] ) ) {
echo '<span class="face">' . woocommerce_get_product_thumbnail() . '</span>';
echo '<span class="face back">';
yit_image( "id=$attachments[1]&size=shop_catalog&class=image-hover" );
echo '</span></a>';
}
else {
echo '<span class="face">' . woocommerce_get_product_thumbnail() . '</span>';
}
break;
}
Code Added:
if( $product->is_type( 'external' ) ){
$product_url = $product->get_product_url() . '"target="_blank""';
} else( $product_url = get_permalink());
} else ($product_url = get_permalink());
Code Modified:
With the exception of the code that was added above, replace all instances of get_permalink() with $product_url.
Figured out a work around. Since the SalesFlash image was the one being triggered, I just used a blank PNG image to overlay ontop of the product image. Turned all my products into sale items and it works. Not perfect, but I don't need the sale icon anyway.
But if anyone does know of a proper programming solution, I would change it. Thanks.
this on worked for me in the content-product.php without asking the meta data
<div class="thumbnail-wrapper"><a href="<?php echo $product->product_url; ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
?> </a>
</div
I did the same in thing in loop/add-to-cart.php for the "Add More"
and "Details" buttons in lines 21 get_permalink() and 57 $link by replacing them respectively like this:
LINE 27
$details = sprintf('%s', get_permalink(), apply_filters('yit_details_button', __( 'Details', 'yit' )), apply_filters('yit_details_button', __( 'Details', 'yit' )) );
REPLACE get_permalink() with $product->product_url
AND IN LINE 57
$add_to_cart = sprintf('%s', apply_filters( 'yit_external_add_to_cart_link_loop', $link, $product ), $label, $label);
REPLACE again $link with $product->product_url .
I had no problems untill now. I was wondering if you Finally found a clear solution so we could do the same thing for thumnails and product images ,without adding a blank image on top of them.Im not much of code so i would appreciate if someone Knows how to put an external link on the product images on the front page.Thank you

Categories