PHP Code
A PHP code widget allows you to "run" a PHP script directly in any template or custom page. This works much like hooks, except that they are not triggered only if a certain task is run.
Installing a PHP Code Widget
- Start by uploading an edited version of the widget installation script into the "custom/widgets" folder. Name this file install.php. Make sure you edit the three widget variables near the top of the file!. The $widget_type should always be "code". The $widget_id needs to be unique.
- Go to your Zenbership admin control panel and click on "Content > Widgets > Run Installer". You should see a "Success" dialogue.
- Create a PHP file with the same name as your $widget_id (set up in the installer script). So if your widget ID is "my_calendar_script", you would create a file called "my_calendar_script.php" and upload it to the "custom/widgets" folder.
- Add the widget inclusion code for the widget onto any template or custom page to run the script.
Sample PHP Widget
Note that the program's standard environment is already loaded for PHP widgets!
Example 1: Query the User's Data
The following is a sample that will provide you with a user object for the user viewing the page.
<?php // Get the user's session and confirm // that he/she is logged in. $session = new session(); $check_session = $session->check_session(); if ($check_session['error'] != '1') { // Get the user's information $userObj = new user; $user_data = $userObj->get_user($check_session['member_id']); // "Dump" the user data. pa($user_data); // Display the user's username and email. echo '<br />Username: ' . $user_data['data']['username']; echo '<br />E-Mail: ' . $user_data['data']['email']; } ?>
Example 2: Print A Calendar
The following is a sample PHP widget which will print a calendar with "events" on April 20th and April 25th:
<?php $data = array( 'put_dates' => array( '2014-04-20' => array( 'Show something', ), '2014-04-25' => array( 'Event 1', 'Event 2', ), ) ); $calendar = new calendar('', '', $data, 'user'); $calendar->generate(); echo $calendar->output; ?>
Article Links
- Installing a PHP Code Widget
- Sample PHP Widget
- Example 1: Query the User's Data
- Example 2: Print A Calendar
Related Articles
Article Tags
Details
Published on 2013/07/10.
Last updated on 2014/04/18.