WordPress.org

Ready to get started?Download WordPress

Forums

How to echo adsense code in a div with PHP? (5 posts)

  1. shrinitech
    Member
    Posted 8 months ago #

    How to echo AdSense code with PHP? Here is the sample of my code which I am working for a wordpress plugin.

    <div class='mydiv'><?php echo stripslashes($adsensecode)?></div>

    All I want to insert adsense code inside a div with PHP. I also tried with htmlentities along with stripslashes but ad in not getting displayed.

  2. byankov
    Member
    Posted 8 months ago #

    You should echo the code directly.
    If you want to escape user input in the html, you must use the htmlspecialchars php function on the adsense id for example, but not for the full code bacause it contains special characters.

  3. shrinitech
    Member
    Posted 8 months ago #

    Thank you for your reply byankov.. May I have the example code please? I have given the text area in my plugin option page where users can add their adsense code.. I want to display that ad in a wordpress blog post.

  4. alchymyth
    The Sweeper & Moderator
    Posted 8 months ago #

    I have given the text area in my plugin option page where users can add their adsense code.

    what plugin?

    I want to display that ad in a wordpress blog post.

    do you have php code enabled in the post content, for example via a plugin?

    have you considered to add the code via a filter for 'the_content'?

    have you considered to create a shortcode for the adsense code?

  5. shrinitech
    Member
    Posted 8 months ago #

    Dear alchymyth,

    I am creating a new plugin, added a textarea in the option page of my plugin so that users can add their adsense ad code, everything is ready, plugin receives the saved adsense code in a variable $adsensecode, finally I am using jQuery to display the same adsense advertisement inside <article>, here is the part of the code-

    jQuery("article p:eq(1)").before("<div class='mydiv'><?php echo($adsensecode); ?></div>");

    but adsense advertisement is not getting displayed. If I write some lorem ipsum text in option page, it displays but the adsense code is not outputting the advertisement.

Reply

You must log in to post.

About this Topic