Need help with DFP ad tag injection

Can you please help me to figure out how can I inject dfp tags above the content? Like the section mentioned in the picture?

URL: Learnmechanical.com

@learnmechanical2019,

Thanks for writing in.

To inject something above the content would require custom coding and it is outside the scope of our theme support. You may seek 3rd party developers to help you with your concern or you can avail our One.

You can also check our theme Actions and Filters docs that might help you with your concern.

Also, we’re creating a Layout Builder now to allow the users to build a custom archive and single pages. That means you won’t need custom code in the future.

Hope that helps and thank you for understanding.

Hi thanks for your message, however, can you guide me with the theme file path, where I can put my script, it will be more appreciable.

And 2ndly is the layout builder which you have addressed in the previous message is available for X theme or I need a pro?

Hi Saswata,

Its an action hook function, so you can place it on a child theme’s functions.php file. The action that you need is x_before_the_content_begin or x_after_the_content_begin this will put your code above the content. You can follow the examples as guide provided here. Since this is in the realm of custom development it would ultimately be your responsibility to take it from here.

The Layout Builder would be available for PRO for sure, but we don’t have concrete plan if it would be available with X, probably not.

Cheers!

Hi @friech thanks for guiding me, just a last question, can you please verify the action:

>     function x_before_the_content_begin() { ?>
> 
>       My DFP Script
> 
>     <?php }
> 
>     add_action( 'dfp_ad_tag', 'x_before_the_content_begin' ); 

Is it okay?

Hi Saswata,

As mentioned in earlier answer, the action hook need to called x_before_the_content_begin and x_after_the_content_begin. These are predefined hooks of our theme.
You need to call this hooks in the add_action function and the first parameter should be the action hook and the second one will be the custom function name.
You can learn more on add_action from here: https://developer.wordpress.org/reference/functions/add_action/

Please find correct way to write your one:

function dfp_ad_tag() { ?>
 
       Your DFP Script
 
     <?php }
 add_action( 'x_before_the_content_begin', 'dfp_ad_tag' ); 

Hope it helps

Thanks

1 Like

Thanks much everyone, again sorry to interrupt you, I wanna insert this function on the posts only, I tired using adding this “if (is_singular(‘post’)) {” but there is problem in some-line, so can you please help me with that, how the structure will be. I totally understand it is a little custom development, but still, just to guide me to the right direction.

Much thanks!

Hello Saswata,

You should be using [is_single()](https://developer.wordpress.org/reference/functions/is_single/). Therefore your code could be:

function dfp_ad_tag() { ?>

	<?php if ( is_single() && 'post' == get_post_type() ) : ?>
 
       Your DFP Script

    <?php endif; ?>
 
     <?php }
 add_action( 'x_before_the_content_begin', 'dfp_ad_tag' ); 

Hope this helps.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.