Jump to content
  • 0
bear

Admin home widget, file contents

Question

Is it possible to include the contents of a file in an admin area home page widget?
I've been calling an external API to Namecheap for various data points, and find now that I have more than a few it's slowing the admin home page load some, especially if NC is slow responding. my plan is to take those API calls and have them write results to a file a few times per day (cron based), and simply have WHMCS display the contents of that file instead of a live call. 

There are 4 different things I'm currently calling live, so I'd need to call 4 different files into the widget. 
Is that possible?

Share this post


Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 0
9 minutes ago, bear said:

Clearly I'm doing something wrong.

if this is v6 or earlier, the return array should be content...

return array('title'=>$title,'content'=>$output);

if it were v7, you'd do it another way too... though v6 widgets will usually work in v7. :idea:

also, when I was linking to the demo.txt file, I used absolute URLs rather than relative paths (file was on a different server)... but I suspect your issue was with the return array. :smile2:

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
19 hours ago, bear said:

Is it possible to include the contents of a file in an admin area home page widget?

yes - a number of ways.

19 hours ago, bear said:

There are 4 different things I'm currently calling live, so I'd need to call 4 different files into the widget. 
Is that possible?

or just create one file from the four sources and call that once in the widget? difficult to say which would be easier without knowing the content, but it might not matter.

for the sake of argument, let's say we have a text file on the server containing three lines...

Quote

the quick brown fox jumped over the lazy dog

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

$name1 = file("path/to/file/demo.txt");
foreach ($name1 as $key => $line) {
	$output .= 'Line '.$key. ' : ' . $line . '<br />';		
}

DI63Zgu.png

ignore the other tabs(!), you can see the widget is pulling the text file contents into an array, and then you can output the array via a loop (or directly if you know which line the info you want is on).

Share this post


Link to post
Share on other sites
  • 0

Thanks, Brian. 

The API calls are separate from each other and AFAIK can't be "stacked" into one, so it is likely necessary to have separate files. 
The one lists domains due to expire within the next 30 days, each on it's own line, so listing per line won't work there. Basically I'd like to have one block for "Namecheap", with the 4 returned results in it like this:

Account Balance: (from file, one line)
Total domains in account: (from file, one line)
Expired name(s): (from file, may be more than one line)
Expiring names: (from file, often more than one line)

I won't be needing the foreach for the lines, since it will already be in the format of one line each with BR tags, since the XML parser does the heavy lifting. 
Would that be as simple as an include at that point?

Share this post


Link to post
Share on other sites
  • 0

let's say for simplicity I change demo.txt to the following... (and for this example 1 array with 4 lines is the same as 4 arrays of 1 line)...

Quote

123.45
12
bear.com<br>bear.co.uk
brian.com<br>brian.co.uk

then your widget code could be...

		$output .= '<div>';
		$output .= 'Account Balance: '.$name1[0];		
		$output .= 'Total domains: '.$name1[1];		
		$output .= 'Expired name(s): '.$name1[2];		
		$output .= 'Expiring name(s): '.$name1[3];		
		$output .= '</div>';

DrvNC43.png

now if you were using 4 separate files, then you would define each individually (the same as I did with $name1) and then use $name2[0], $name3[0] and so on in the widget code.

... or perhaps more suitable for you, you could just call the file directly in the output (if you can trust the source and don't need to do anything to it) for each of the 4 files...

$output .= 'Expiring name(s): '.file_get_contents("path/to/file/demo4.php");	

SYbzAYY.png

Share this post


Link to post
Share on other sites
  • 0

No joy there. Files are where they're supposed to be, called as mentioned, no content. 

<?php

function widget_namecheap_includetest($vars) {
    global $_ADMINLANG;
    $title = "Namecheap Include Test";

$output = '';
$output .= 'Account Balance: '.file_get_contents("/home/xxx/namecheap/file1.txt").'<br>';
$output .= 'Total Domains: '.file_get_contents("/home/xxx/namecheap/file2.txt").'<br>';
$output .= 'Expired name(s): '.file_get_contents("/home/xxx/namecheap/file3.txt").'<br>';
$output .= 'Expiring name(s): '.file_get_contents("/home/xxx/namecheap/file4.txt");

	 return array('title'=>$title,'output'=>$output);

	}

add_hook("AdminHomeWidgets",1,"widget_namecheap_includetest");

?>

Clearly I'm doing something wrong.

Share this post


Link to post
Share on other sites
  • 0

That was it, Brian. Thanks! Using "content" it popped right in. 
Some days I just can't wrap my head around stuff. 

Onward to grabbing the actual content now!

Share this post


Link to post
Share on other sites
  • 0

Got this completed, and it's working flawlessly. 

Crons set to poll the Namecheap API periodically for each item instead of each home page load, and it displays the files in the admin home page widget as planned. Onward. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using this site, you agree to our Terms of Use & Guidelines