Jump to content
Sign in to follow this  
cdeese8

best way add php snippet on tpl page

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/

 

 

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites
18 hours ago, cdeese8 said:

I'm looking to "add recent wordpress posts to non wordpress site"

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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
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'){

 

Share this post


Link to post
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.

Share this post


Link to post
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.

Sign in to follow this  

  • Similar Content

    • By JesusSuarz
      Hello
      I have a variable that is printed with {debug} in the tpl template.
      however I want to get the result in php of this variable.
      I need to get $customfields from a hook.
      how could you access $customfields that is printed inside a product
       
      @brian! could you help me?
    • By ModulesGarden
      1. Plesk Extended For WHMCS 3.5.0

      Because your system can never suffer from too many user-friendly features, today we are storming ahead with a solid update of Plesk Extended For WHMCS 3.5.0 that is about to nurture your web hosting business in more than a few different ways:
       
      Take maximum advantage of the newly introduced support for WHMCS V8.1 and "Twenty-One" theme. In order to limit the system resources consumed by PHP scripts, your clients are now allowed to use a custom PHP version and configurations per website. While in the comfy surroundings of the client area, let us also move a bit further to the new "Log Rotation" section where your customers can manage conditions for the log files to be cleaned up once they are too old or too big.
      Be sure to enjoy the full changelog available on our website!



      2. SMS Center For WHMCS 3.8.0

      Just as much deserving of a special mention is SMS Center For WHMCS, specifically because of its spanking-new 3.8.0 update!

      The module’s scope of support has been broadened to integrate well with as many as five all-new SMS gateways. Thanks to it, the official list of available SMS gateways now includes 51 providers in total!

      Additionally, the product has also received a series of tweak-ups so as to secure its rock-solid compatibility with both WHMCS V8.1 and "Twenty-One" system theme.

      Keep in touch with your customers no matter the time and place!



      3. Under the Microscope: MetricsCube Live Dashboard for WHMCS

      Reading through your business data feels as though you were reading a foreign language?

      Live Dashboard for WHMCS that forms a central part of MetricsCube communicates crucial metrics in a visual format to make all complex relationships in your data self-explanatory. Drop by the MetricsCube blog as soon as you can to get a close-up look at every single report and graph illustrating the real-time performance of your WHMCS business down to the last detail.

      Take an insightful stroll through the data-filled confines of Live Dashboard!



      Need Custom Software Development For Your Business?
       
      Get Your Free Quote Now! Specially for you we will adapt an application and its design to your own needs, create a new module or even a completely new system built from scratch!
    • By Kianzo
      I found this topic
       
      https://whmcs.community/topic/283057-adding-links-to-shortcut-area/
      which is so awesome and allows to create a link to the wp-admin of the client domain
       
       
      <?php use WHMCS\View\Menu\Item as MenuItem; add_hook('ClientAreaPrimarySidebar', 1, function(MenuItem $primarySidebar) { $service = Menu::context('service'); $domain = $service->domain; $servertype = $service->product->servertype; # Not cPanel, no links added if ($servertype!="cpanel"){ return; } if (!is_null($primarySidebar->getChild('Service Details Actions'))) { $primarySidebar->getChild('Service Details Actions') ->addChild('wplink', array( 'label' => 'Link To WordPress', 'uri' => 'https://'.$domain.'/wp-admin', 'order' => 200, )); } });  
      BUT  how to do it if the client has more than one domain?
      let's say that my client has 5 domains and each domain has it's own WordPress install
      so how  to add all of them to the navbar menu as a sub menu?
       
      PS: all customers have domains registered with GoDaddy. I host WordPress websites for them only.
      Thank you ♥
    • By JesusSuarz
      sometimes the client can have 2 emails.
      one for your account details and one for the account owner.
      I need to know if the account owner's email is verified.
      The var: 
      $client = Menu::context("client"); gets the email that the customer has in the account details, however this is not the email owner of the account.
      I hope to make myself understood correctly.
       
    • By Eugene.Viljoen
      Hi,
      Sorry if this has been asked before but i can seem to find any solution for it!
      I am looking for a way to display a "Network Status" icon/ css on the home page of my website based on the WHMCS network status rss feed (https://portal.whmcs.com/networkstatusrss.php).
      The concept is to have the icon/css auto change when one of the following network issues are opened rather then having to change it manually.
      Low Medium High Critical Attached is an illustration of what i am looking at doing.
      PS: I already wrote the css to have pluse icons, just need to implement the second part.
      Thank you in advance! 😁
       
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • 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