Custom Twitter button did not process php tags

Hi everyone,

I need to include a #hashtag in the content of tweet, so I used a custom field to try achieve this.

My custom code looked like this:

<a href="https://twitter.com/share" class="twitter-share-button" data-lang="pt_BR" data-url="<?php echo get_permalink(); ?>" data-via="variavel5" data-count="vertical" data-text="Participe do projeto <?php the_title(); ?> #<?php echo get_post_meta($post->ID, 'hashtag', true); ?>">Tweetar</a>

But the rendered content did not process the php tags, like this:

Participe do projeto <?php the_title(); ?> #<?php echo get_post_meta($post->ID, 'hashtag', true); ?> via @variavel5

Anyone please help me how can I do this?

Thanks in advance.

  • aecnu

    Greetings bruno_brant,

    Thank you for the great question and custom coding which is indeed beyond the scope of the support that we provide here though we try to help if we can.

    With that said I will see if I can get the lead developer in here with his invaluable insight into this plugin for his advice/advise for us but no promises that he will be able to resolve this - I cannot speak for him of course but it is certain we will try.

    Though this may take a bit longer then a normal ticket, I will try to get him in here asap.

    Cheers, Joe

  • Vladislav

    Hello,

    The custom services allow for javascript and html, but won't process any PHP code entered. Allowing this would be quite risky, and we won't be supporting inline PHP in custom services. Having said all that, there might be a way to get this to work the way you want. It would be a two-step process, involving a "template" custom service, and a javascript that would populate the things you need on the fly.

    First off, the custom service. This is the code we'll use in the plugin settings, when adding the custom service:

    <a href="https://twitter.com/share" class="my_two_step_rewite_service" data-lang="pt_BR" data-via="variavel5" data-count="vertical" data-text="Participe do projeto">Tweetar</a>

    As you see, it's a blank template, similar to the one you currently using. The important moment is the custom class, which we'll use to populate the data you need and make it a Twitter link.

    Next, we'll need some PHP and javascript. This code can go in a mu-plugin file or your functions.php - it will populate your template service:

    function my_2step_rewrite () {
    	if (!is_singular()) return false;
    	global $post;
    	$permalink = get_permalink();
    	$title = get_the_title();
    	$hashtag = get_post_meta($post->ID, 'hashtag', true);
    	echo <<<EoMy2StepRewriteJs
    <script type="text/javascript">
    (function ($) {
    $(function () {
    	$(".my_two_step_rewite_service").each(function () {
    		var me = $(this);
    		me
    			.attr("data-url", "{$permalink}")
    			.attr("data-text", "Participe do projeto {$title} #{$hashtag}")
    			.addClass("twitter-share-button")
    		;
    	});
    });
    })(jQuery);
    </script>
    EoMy2StepRewriteJs;
    }
    add_action('wp_footer', 'my_2step_rewrite');

    Provided your theme triggers the wp_footer hook, this should be it. Please note that this assumes you already have "//platform.twitter.com/widgets.js" script present on your page - if not, you'll want to include it too (possibly in your footer.php).

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.