Jump to content

best way add php snippet on tpl page


cdeese8

Recommended Posts

What's the best way to add a snippet of PHP inside a .tpl file? It looks like everybody recommends against using the {php}{/php} bracket for security reason. Should I be creating my .php snippet file in root WHMCS directory and just using include to beam it in?

{php}
include "my-php-snippet.php"; 
{/php}

I'm looking to "add recent wordpress posts to non wordpress site" and they all involve adding php snippet to include the wp-load.php file. It'd be cool if I can avoid using an <iframe> too. This is the snippet I'd like to add a .tpl page:

<?php
// Include the wp-load'er
include('/public_html/wordpress/wp-load.php');

// Get the last 10 posts
// Returns posts as arrays instead of get_posts' objects
$recent_posts = wp_get_recent_posts(array(
	'numberposts' => 10
));
	
// Do something with them
echo '<ul>';
foreach($recent_posts as $post) {
	echo '<li><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></li>';
}
echo '</ul>';
?>

* whmcs 7.4 is installed on root domain, and the wordpress site is installed in a subfolder
https://davidwalsh.name/wordpress-recent-posts
https://www.webmoves.net/blog/web-design-development/display-wordpress-posts-outside-of-wordpress-3081/

 

 

Link to comment
Share on other sites

It is security but also programming standards as logic should be limited to controllers (and models) in the MVC programming style.  So php should be left out of templates as templates are in the View context.   It also helps to limit customizing template files.

So with that said, it would be best to use a hook such as the ClientAreaFooterOutput , get the wordpress stuff as you describe within that hook's function, and then use javascript to move the output to where you want. 

For example:

<?php

add_hook('ClientAreaFooterOutput', 1, function($vars) {

// Include the wp-load'er
include('/public_html/wordpress/wp-load.php');

// Get the last 10 posts
// Returns posts as arrays instead of get_posts' objects
$recent_posts = wp_get_recent_posts(array(
    'numberposts' => 10
));
    
// Do something with them
$Text = "<ul id='wp-recent-posts>";

foreach($recent_posts as $post) {
    $Text .= '<li><a href="'. get_permalink($post['ID']). '">'. $post['post_title']. '</a></li>';
}
$Text .= '</ul>';

$Text .= '$(document).ready(function() {$("#wp-recent-posts").prependTo("#wp-container") })';

return $Text;


});

then just place in file called "hook_wp_recents.php" in whmcs install/includes/hooks .  It will then display them on every page within the client area.  You can limit which page, or at least template, it injects in to by checking $vars['filename'].  Or checking the url ,etc.  Updates to WHMCS should not break this and you don't have to redo the templates each time. 

Link to comment
Share on other sites

  • 2 weeks later...
On 11/17/2020 at 11:58 AM, brian! said:

is using a WordPress RSS feed an option ? if it exists, that content could be shown in the footer, sidebar, panel etc...

I believe it, absolutely. I was not able to "control the positioning" of the above shared snippet. I was hoping to be able to add a hook on a specific page too, maybe inside one of those .tpl files.

So it looks like best way to add PHP code is to use a hook. BUT, if using a hook, how do we control the exact page to display the code on? Or even make it so I can re-use the code using one of those smarty shortcodes?

Thanks for everything and the help too btw, very nice!

Link to comment
Share on other sites

On 11/16/2020 at 4:29 PM, steven99 said:

then just place in file called "hook_wp_recents.php" in whmcs install/includes/hooks .  It will then display them on every page within the client area.  You can limit which page, or at least template, it injects in to by checking $vars['filename'].  Or checking the url ,etc.  Updates to WHMCS should not break this and you don't have to redo the templates each time. 

How do I "check"  $vars['filename'] or check the url? I've browsed a few topics on the forums and noticed Brian! shared a few snippets.... 

    $filename = APP::getCurrentFileName();
    if ($filename=="clientarea" && $_GET['action']=="details"){

or

if (APP::getCurrentFileName() == 'clientarea' && $_GET['action'] == 'details'){

 

Link to comment
Share on other sites

22 hours ago, cdeese8 said:

I believe it, absolutely.

good news.

22 hours ago, cdeese8 said:

I was not able to "control the positioning" of the above shared snippet. I was hoping to be able to add a hook on a specific page too, maybe inside one of those .tpl files.

to do that, you would need to go further with the coding....

22 hours ago, cdeese8 said:

So it looks like best way to add PHP code is to use a hook. BUT, if using a hook, how do we control the exact page to display the code on? Or even make it so I can re-use the code using one of those smarty shortcodes?

one option would be to use JS to identify the location that you want to output your content... but then you're dependent on being able to locate that unique spot via the existing page code etc.. and WHMCS often doesn't use unique IDs, so it can be an absolute pain to put content in the spot you want using this method.

another option would be to create your content in a variable, e.g your snippet content, pass that variable back to the template and then edit the template to output the content wherever you want to.

as an example, years ago I wrote a hook for a client who wanted to output their WP Feed as a homepagepanel in their footer - that code would still work in v8... as an example using BBC Feeds...

wrw0fYx.png

the point being that once you have a feed and can parse it as required, then you can output it however you like (within reason!) - you wouldn't necessarily need to make it look like a home page panel, but style it to your own needs.

in the above example, i'm just adding {$rssfeed} to the footer template where I want the output to be - though in this case, I could equally have done that with JS and no template edit...

21 hours ago, cdeese8 said:

How do I "check"  $vars['filename'] or check the url?

if the hook point includes $vars, you should be able to use...

if ($vars['filename'] == "xxx") {

or if you need to specify a specific template...

if ($vars['templatefile'] == 'viewcart') {

or you use a specific hook point that only works on the page you want to modify.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated