Okay
  Public Ticket #176183
shortcode to insert php
Closed

Comments

  • rico started the conversation

    Hello,

    I would like to know if there is a way to insert PHP using a shortcode. I'm using Facebook api (PHP) to display the number of my Facebook Page fans just before the footer (fullwith + background image).

    It"s work if I write the PHP in the footer.php file, but I would prefer to do all my customizations without altering the original theme files.

    A PHP shortcode will be very helpful since I could display it only on the home page and I don't have to worry when updating Salient.

    I tried the "raw HTML" and "JS" shortcodes but it doesn't work.

    One more thing, is there a way (CSS class or JS) to animate the number of fans generated buy the PHP like in milestone ?

    Thanks in advance.


    Rico

  • rico replied

    Hello,

    I can insert PHP code with shortcode (text editor) by using Exec PHP Wordpress plugin, but if you know a better and cleaner way, I'll be happy to know it.

    If you have time to answer my other question about number animation.

    Thanks

    Rico

  •  983
    ThemeNectar replied

    Hey Rico,

    Glad you sorted the PHP query - and yes, that method is perfectly fine.

    In regards to the animation of a number you can just place your number and text inside of this:

    http://pastebin.com/1xUEVJ6D
    

    That's the raw html which will trigger the milestone animation which you can feed any number you want from any source now that you can use PHP in the editor :)

    Cheers :)


  • rico replied

    Hey Nectar,

    That's works, thank you very much ^_^ !

    Feel free to make this public if it can help others.

    Have a great week.

    Rico.

  • Andrew replied

    hey rico! how exactly did you get this to work? I want to pull my number off of an xml file from http://gdata.youtube.com/feeds/api/users/deadlydragon218 to get the subscriber count of my channel however I just can't seem to get it working in wordpress, out side of wordpress my php works great! however the moment I try to put it into the visual composer i get NaN not a number, I even tried changing the output to an integer http://deadlydragongaming.com is the page btw :)

  • rico replied

    Hi Andrew did you try this ?

         <div class="nectar-milestone">
               <div class="number extra-color-1">65 (number generated by PHP)</div>
               <div class="subject">YouTube followers</div>
         </div>
    
  • Andrew replied

    Well what I want to have happen is, that I wont have to manually change the number anymore, btw I am new to how php works, so what i am doing is 

        <div class="nectar-milestone">
            <div class="number extra-color-1">
    <?php
    echo youtube_subscribers("deadlydragon218");
    function youtube_subscribers($user)
    {
        $xml = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . strtolower($user));
        $xml = str_replace('yt:', 'yt', $xml);
        $xml = new SimpleXMLElement($xml);
        $subs = $xml->ytstatistics['subscriberCount'];
        return(trim($subs));
    }
    ?></div>
        <div class="subject">YouTube followers</div>
    </div>
    just so you can see what my code is, the php works great! I just need to find a way to pass that number as a number and not a string I think that is my current problem, as the milestone code is looking for an integer value I believe, I just want to pass the php value through this visual, that way I no longer have to change it manually everytime I get a new subscriber
  • Andrew replied

    I got it to work! turns out it was nub coding xD

    <?php

    $name = 'deadlydragon218';

    $url = sprintf('http://gdata.youtube.com/feeds/api/users/%s?alt=json', urlencode($name));

    $json = @file_get_contents($url); // Naughty @-operator, use proper error handling

    $data = json_decode($json, TRUE);

    $count = (int) $data['entry']['yt$statistics']['subscriberCount'];

    ?>

    <div>

    <div>

    <span><?php echo $count ?></span></div>

    <div>Hatchlings</div>

    </div>

    I forgot to echo the $count I was just placing $count in their xD 

  •  8,425
    Tahir replied

    Thanks for posting back. 

    Cheers


    ThemeNectar Support Team