Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Seb Soithongsuk
Seb Soithongsuk
2,190 Points

Could someone help me dissect this short PHP function so I can understand it please

My PHP expertise is very limited, but I'm working in WordPress and looking at dynamically serving up HTML with this plugin called WP Mobile Detect.

Just for my own development, I want to understand how the PHP function works.

So basically, all you need to do is wrap any content you like in WordPress in shortcode tags e.g. "[phone]This content will only appear on a smartphone[/phone]" and the content contained will not be downloaded to the browser unless it is viewed on a smartphone.

So now, the plugin has this wp-mobile-detect.php file and here's an extract of the phone functionality I described...

/********************************************//**
* Generates [phone][/phone] shortcode which shows content on phones ONLY
***********************************************/
function wpmd_phone( $tats, $content="" ) {
    global $detect;
    if( $detect->isMobile() && ! $detect->isTablet() ) return do_shortcode($content);
}
add_shortcode( 'phone', 'wpmd_phone' );



/********************************************//**
* Returns true when on phones ONLY
***********************************************/
function wpmd_is_phone() {
    global $detect;
    if( $detect->isMobile() && ! $detect->isTablet() ) return true;
}

Could anyone break each line down one by one to explain how it works?

Thanks.

2 Answers

First of all, I think you are missing something at the top of the file that says something like this:

include 'php/Mobile_Detect.php';

$detect = new Mobile_Detect();

If so, you have to start looking there. The include is a reference to another file that has the main function code for detecting the phone. The next line assigns the function Mobile_Detect() that is contained in the file Mobile_Detect.php to the variable $detect.

Then the code that you quoted first creates a new function called wpmd_phone. That function accepts two parameters, $tats and $content.

That function calls the Mobile_Detect function through the variable.

Then if the return of the detect function isMobile() is true and the return detect function isTablet() is false, then the function returns the do_shortcode function with the parameter $content.

After the function is complete, then it executes the add_shortcode function with parameters 'phone' and 'wpmd_phone'.

That function has to be called from somewhere else in the code from what I can see.

Then there is a second function that returns true when on phones only. It calls the function assigned to $detect. Then if that function returns true for isMobile() and false for isTablet(), the function returns true.

There is a lot of functions missing from the quote to understand how it really is functioning.

Seb Soithongsuk
Seb Soithongsuk
2,190 Points

Yep you're right about those two lines at the top. The mobile_detect.php file looks like it has some sort of lookup table functionality basically. Not too concerned with that right now.

I realise now that the add_shortcode and do_shortcode functions are some standard WordPress functions, so I can look them up in other documentation.

Many thanks for the detailed breakdown - it clears this up a lot and now I feel a bit daft for missing out some of the obvious parts.

The add_shortcode and do_shortcode may not be standard functions. Google them and you should find some good info.

Seb Soithongsuk
Seb Soithongsuk
2,190 Points

I believe they are. I haven't looked into these pages in detail yet, but if anyone else is interested the WordPress codex has documentation on these two functions:

https://developer.wordpress.org/reference/functions/add_shortcode/ https://developer.wordpress.org/reference/functions/do_shortcode/