I'm trying to figure out how to echo a variable inside a variable.
This code below doesn't obviously work because I'm not echoing the variable
$tweet = get_field('tweet_msg'); //this is getting the string inputted by user in the custom field
$tweet_intent = '<div>TEST </div>';
but when ever I do PHP throws an error saying unexpected echo:
$tweet_intent = '<div style="margin-bottom:15px;">TEST </div>';
Full code:
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$tweet = get_field('tweet_msg');
$tweet_intent = '<div style="margin-bottom:15px;">TEST </div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $tweet_intent, 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 );
}
The problem reside in your <a href> syntax.
Assuming that, after get_field(), the value of $tweet is 'Hello-World', your code:
$tweet_intent = '<div style="margin-bottom:15px;">TEST </div>';
put in $tweet_intent this string:
(...)TEST </div>
└──────────────────────────────────────┘
As you can see, the quotation marks of href are closed before $tweet output.
You have to change your code in this way:
$tweet = get_field( 'tweet_msg' );
$tweet = rawurlencode( $tweet ); // only if encoding is not performed by get_field
$tweet_intent = '
<div style="margin-bottom:15px;">
TEST
</div>';
Related
I got a Warning message on my WordPress site trying to insert a featured image after the first paragraph. It worked perfectly without warning or error in PHP7.4 but got the below warning on PHP 8.1
Please would need some help thanks in advance.
Warning: Undefined variable $post in /www/wwwroot/.../wp-content/themes/..../functions.php on line 7
Warning: Attempt to read property "ID" on null in /www/wwwroot/.../wp-content/themes/...../functions.php on line 7
add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
$feat_img = get_the_post_thumbnail($post->ID, 'post-single');
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( '<div class="top-featured-image">' . $feat_img . '</div>', 1, $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're trying to access a $post variable through $post->ID tho you didn't define it before.
We could first fetch the post object through get_post() then retrieve the ID, but we can do better. We can use get_the_ID() to retrieve the post ID.
<?php
add_filter( 'the_content', function ( $content ) {
if ( ! is_admin() && is_single() ) {
$thumbnail = get_the_post_thumbnail( get_the_ID(), 'post-single' );
return prefix_insert_after_paragraph( '<div class="top-featured-image">' . $thumbnail . '</div>', 1, $content );
};
return $content;
}, 20 );
I am trying to insert an advert before the first h2 tag in a post. The code I have tried is below, but the advert comes after the content and not before. I'm looking to insert the advert before the content, and not after.
/*Insert ads after second paragraph of single post content.*/
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = 'This is my ads';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 1, $content );
}
return $content;
}
//Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</h2>';
$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 );
}
Any help would be appreciated.
Try this:
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '<h2>';
$paragraphs = explode( $closing_p, $content );
if ($paragraphs) {
$paragraphs[0] = $paragraphs[0].$insertion;
}
return implode( '<h2>', $paragraphs );
}
Like the title says. I’ve successfully placed adsense ads on my AMP posts. While testing it out on my desktop via firebug, adverts are coming up as intended. However, I cannot get any ads to come up on my mobile? The pages body class changes to amp-mode-touch and the adverts div containers disappear completely.
Here is an example URL - http://www.gamersgreed.com/kingdom-come-deliverance-fps-boost-optimization/amp
And here is the code i'm using to produce the ads -
//Insert ads after third and seventh paragraph of any single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div style="text-align: center;"><amp-ad width=300 height=250
type="adsense"
data-ad-client="ca-pub-7568065840567088"
data-ad-slot="1234194857">
<div placeholder></div>
<div fallback></div>
</amp-ad>
</div>
';
$ad_code2 = $ad_code;
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph( $ad_code, 2, $content );
$content = prefix_insert_after_paragraph( $ad_code2, 7, $content );
return $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 );
}
I have a problem with getting the first form the_excerpt();
Function acutally works but only for first post. I added in functions.php
function get_first_paragraph(){
global $post;
$str = wpautop( get_the_content() );
$str = substr( $str, 0, strpos( $str, '</p>' ) + 4 );
$str = strip_tags($str, '<a><strong><em>');
return '<p>' . $str . '</p>';
}
I'm calling this funcion in index.php inside The Loop <?php echo get_first_paragraph(); ?>
I have no idea why it pulls only for first post...
you can put this code in function.php file in your theme,
Get first paragraph
function awesome_excerpt($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
$text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
}
return $text;
}
add_filter( 'wp_trim_excerpt', 'awesome_excerpt', 10, 2 );
For more information, you can follow the reference link WORDPRESS THE_EXCERPT SHOW ONLY FIRST PARAGRAPH
That code didn't work for me in Gutenberg times. So I've used part of that code and did some searching and come up with this solution. Hope it helps.
function get_paragraph_content($paragraph_number){
global $post;
$i = 0;
$paragraph = '';
if ( has_blocks( $post->post_content ) ) {
$blocks = parse_blocks( $post->post_content );
foreach( $blocks as $block ) {
if( 'core/paragraph' === $block['blockName']){
$paragraph = render_block($block);
if (++$i == $paragraph_number) break;
}
}
$paragraph = substr( $paragraph, 0, strpos( $paragraph, '</p>' ) + 4 );
$paragraph = strip_tags($paragraph, '<a><strong><em>');
}
return $paragraph;
}
Here is a simple code which shows the Adsense or any ad code after second paragraph on single post page. But I want to know how to modify it so that it can run a dynamic function.
//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>Ads code goes 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 );
}
In fact I want to replace
$ad_code = '<div>Ads code goes here</div>';
with
$ad_code = '<?php if(function_exists('echo_ald_crp')) echo_ald_crp(); ?>';
Is there any way to do it??
Well, you can't assign PHP to a string var and expect it to execute. You could create a function with the code in it, have it return something, and then set the variable equal to the function call. Like this:
function output_function() {
//do something here
return $thing_you_want_to_return;
}
function prefix_insert_post_ads( $content ) {
$ad_code = output_function();
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
That way, when the variable is assigned, it'll run your function and return what you want to be echoed out with prefix_insert_after_paragraph().