Using Wordpress Plugin Function in a Widget - php

I have a Wordpress plugin with a file (userinfo.php) that processes a function -- generateUserInfo(). I want to use this function on a sidebar widget. I'm unclear on exactly how to get that done.
Here is the code from userinfo.php inside of my plugin.
require_once("../../../wp-load.php");
//need the current user's ID
$user = get_current_user_id();
$meetings = generateUserInfo($user, 4, 3, 'meetings');
$events = generateUserInfo($user, 6, 2, 'events');
function generateUserInfo($user,$taxonomy_id,$num_required_events,$event_type){
global $wpdb, $table_prefix;
$num_total_events = 0;
$num_actual_events = 0;
//Get the name of the taxonomy term for events (ID: 6)
$events = get_term_by('term_taxonomy_id', $taxonomy_id, 'events_categories');
$event_category_name = $events->name;
$event_posts = get_posts(array('post_type' => 'ai1ec_event', 'events_categories' => $event_category_name, 'posts_per_page' => -1));
foreach($event_posts as $event){
$attended_events_query = $wpdb->get_results("SELECT vote FROM ".$table_prefix."attendance_list WHERE post = " . $event->ID . " AND user = " . $user);
if(count($attended_events_query)>0 && is_array($attended_events_query)) {
foreach($attended_events_query as $evt){
//add to the acutal events attended
if($evt->vote == 1) $num_actual_events++;
}
}
$num_total_events++;
}
$events_msg = '<div>You have attended <br> <span class="big-num">'. $num_actual_events . '</span> / ' . $num_total_events . ' ' . $event_type . '</br>('. $num_required_events.' required)<br><br></div>';
echo $events_msg;
return $events_msg;
}
And here is the beginning of my widget:
function your_widget_display($args) {
extract($args);
echo $before_widget;
echo $before_title . 'Attendance Tracker' . $after_title;
echo $after_widget;
// print some HTML for the widget to display here
echo 'whatever';
}
wp_register_sidebar_widget(
'yac_attendance_widget', // your unique widget id
'YAC Member Attendance Tracker', // widget name
'your_widget_display', // callback function
array( // options
'description' => 'Description of what your widget does'
)
);

Just before echo $after_widget;, add the following
if(function_exists('generateUserInfo')) {
generateUserInfo();
}

Related

I have one functionality that i want to add in single.php page using function in custom plugin. how can i?

I have this code that I want to add in single.php using a custom plugin.
here is the code:
<?php
echo "Facebook: ". $facebook_like_share_count ("$url"). "<br>";
echo "Pinterest: ". $pinterest_pins ("$url") . "<br>";
echo "Google+: ". $google_plusones ("$url") . "<br>";
?>
I want to print this things in single.php when install my custom plugin.
There is two options possible.
Option 1) Create a short code
function bartag_func( $atts ) {
echo "Facebook: ". $facebook_like_share_count ("$url"). "<br>";
echo "Pinterest: ". $pinterest_pins ("$url") . "<br>";
echo "Google+: ". $google_plusones ("$url") . "<br>";
}
add_shortcode( 'social', array( &$this, 'bartag_func' ));
Your short code with be [social]
Option 2 ) Apply the filter on the content
/**
* summary
*/
class WP_Custom_Data
{
/**
* summary
*/
public function __construct()
{
//Here you can add for shortcode as well
add_filter('the_content', array($this, 'wpdev_before_after'));
}
public function wpdev_before_after($content) {
$url = "xyzurl";
$api = file_get_contents( 'http://graph.facebook.com/?id=' . $url );
$count = json_decode( $api );
$beforecontent = $count->share->share_count;
$fullcontent = $beforecontent . $content;
return $fullcontent;
}
// here you can call the shortcode function as well
}
$wpspw_pro_script = new WP_Custom_Data();
Note : I am not sure what you are doing here with function because you have set the variable as a function. according to me it must give you the error.
Anyways, according to me there two option possible here and i have given you the options for same

Alphabetize Petfinder API List

I'm using the Petfinder API for Wordpress plugin. The plugin defaults to listing animals based on how old the Petfinder entries are, from oldest to newest. I'm trying to figure out a way to either do newest to oldest, or alphabetize based on animal names.
The data is loaded via the following code:
function get_petfinder_data($api_key, $shelter_id, $count, $pet = '') {
// If no specific pet is specified
if ( $pet == '' ) {
// Create request URL for all pets from the shelter
$request_url = 'http://api.petfinder.com/shelter.getPets?key=' . $api_key . '&count=' . $count . '&id=' . $shelter_id . '&status=A&output=full';
}
// If a specific pet IS specified
else {
// Create a request URL for that specific pet's data
$request_url = 'http://api.petfinder.com/pet.get?key=' . $api_key . '&id=' . $pet;
}
// Request data from Petfinder
$petfinder_data = #simplexml_load_file( $request_url );
// If data not available, don't display errors on page
if ($petfinder_data === false) {}
return $petfinder_data;
And the code that creates the list looks like this:
function get_all_pets($pets) {
foreach( $pets as $pet ) {
// Define Variables
$pet_name = get_pet_name($pet->name);
$pet_type = get_pet_type($pet->animal);
$pet_size = get_pet_size($pet->size);
$pet_age = get_pet_age($pet->age);
$pet_gender = get_pet_gender($pet->sex);
$pet_options = get_pet_options_list($pet);
$pet_description = get_pet_description($pet->description);
$pet_photo_thumbnail = get_pet_photos($pet, 'medium');
$pet_photo_all = get_pet_photos ($pet, 'large', false);
$pet_more_url = get_site_url() . '/adopt/adoptable-dogs/?view=pet-details&id=' . $pet->id;
$pet_pf_url = 'http://www.petfinder.com/petdetail/' . $pet->id;
// Create breed classes
$pet_breeds_condensed = '';
foreach( $pet->breeds->breed as $breed ) {
$pet_breeds_condensed .= pet_value_condensed($breed) . ' ';
}
// Create options classes
$pet_options_condensed = '';
foreach( $pet->options->option as $option ) {
$option = get_pet_option($option);
if ( $option != '' ) {
$pet_options_condensed .= pet_value_condensed($option) . ' ';
}
}
// Compile pet info
// Add $pet_options and $pet_breeds as classes and meta info
$pet_list .= '<div class="vc_col-sm-3 petfinder ' . pet_value_condensed($pet_age) . ' ' . pet_value_condensed($pet_gender) . ' ' . $pet_breeds_condensed . ' ' . $pet_options_condensed . '">' .
'<div class="dogthumbnail">' .
'' . $pet_photo_thumbnail . '<br>' .
'</div>' .
'<a class="dogname" href="' . $pet_more_url . '">' . $pet_name . '</a><br>' .
'<span> ' . $pet_age . ' • ' . $pet_gender . '<br>' .
'<div class="dogbreed">' . $pet_breeds_condensed . '</div>' .
'<a class="morelink" href="' . $pet_more_url . '">Learn More <i class="fas fa-angle-right"></i></a><br>' .
'</div>';
}
// Return pet list
return $pet_list;
Here's an example of the XML that the Petfinder API spits out (right now there are 25 pet entries in the full thing):
<petfinder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.petfinder.com/schemas/0.9/petfinder.xsd">
<header>
<version>0.1</version>
<timestamp>2018-06-11T17:32:34Z</timestamp>
<status>
<code>100</code>
<message/>
</status>
</header>
<lastOffset>25</lastOffset>
<pets>
<pet>
<id>31035385</id>
<shelterId>IL687</shelterId>
<shelterPetId/>
<name>Chanel</name>
<animal>Dog</animal>
<breeds>...</breeds>
<mix>yes</mix>
<age>Adult</age>
<sex>F</sex>
<size>M</size>
<options>...</options>
<description>...</description>
<lastUpdate>2014-12-14T17:59:49Z</lastUpdate>
<status>A</status>
<media>...</media>
<contact>...</contact>
</pet>
</pets>
</petfinder>
I'd like to sort all entries by either "name" or "lastUpdate". I've been looking at a lot of posts about sorting XML element objects but they either don't seem to work or I can't figure out how to apply them specifically to my code. I'm not super well-versed in this stuff, so any assistance is much appreciated!!
After a LOT of research and trial and error, I figured out how to organize alphabetically by animal name. Posting this in case anybody is ever trying to figure out the same.
First of all, I was wrong in my assumption of which sections I might need to be editing. It was actually line 723 of the plugin file. Here's how I modified the code for that section:
// Display a list of all available dogs
else {
// Access Petfinder Data
$petfinder_data = get_petfinder_data($api_key, $shelter_id, $count);
// If the API returns without errors
if( $petfinder_data->header->status->code == '100' ) {
// If there is at least one animal
if( count( $petfinder_data->pets->pet ) > 0 ) {
//Sort list of dogs ALPHABETICALLY by NAME
$petSXE = $petfinder_data->pets->children();
$petArray = array();
foreach($petSXE->pet as $d) {
$petArray[] = $d;
}
function name_cmp($a, $b) {
$va = (string) $a->name;
$vb = (string) $b->name;
if ($va===$vb) {
return 0;
}
return ($va<$vb) ? -1 : 1;
}
usort($petArray, 'name_cmp');
$pets = $petArray;
// Compile information that you want to include
$petfinder_list = get_type_list($pets).
get_age_list($pets) .
get_size_list($pets) .
get_gender_list($pets) .
get_options_list($pets) .
get_breed_list($pets) .
get_all_pets($pets);
}
This is adapting the solution I found in this thread: sort xml div by child node PHP SimpleXML

WordPress Custom Post Titles

I am looking for a method to change how a post title is shown on screen in WordPress that only applies to the post header.
I am trying to display Name, Gender, Age in the post title. The name I display is the current post title, and I am trying to add gender and age to this for display purposes only.
I have used the current code, but it applies to everything in my theme that uses the tile, and adds these fields to menu items as well.
I have not edited any of the PHP files within the theme, and would like to avoid that and do it via a function
Here is my code:
add_filter( 'the_title', function( $title ) {
$gender = get_field('gender');
$dob = get_field('date_of_birth');
$birthday = new DateTime($dob);
$interval = $birthday->diff(new DateTime);
if ('babysitters' == get_post_type()) {
$temp_title = $title;
$bbstitle = $temp_title .', ' .$gender .', ' .$interval->y;
return $bbstitle;
}
return $title;
} );
What am I doing where it replaces all titles with these appended fields, and not just the post header
UPDATED
function text_domain_custom_title($title) {
global $post;
if ($post->post_type == 'babysitters') {
$gender = get_field('gender', $post->ID);
$dob = get_field('date_of_birth', $post->ID);
$birthday = new DateTime($dob);
$interval = $birthday->diff(new DateTime);
$bbstitle = $title . ', ' . $gender . ', ' . $interval->y;
return $bbstitle;
} else {
return $title;
}
}
add_filter('the_title', 'text_domain_custom_title', 10, 2);
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Please note: This code is not tested, but it should work.
Reference:
get_field
How to append text to title of Custom Post Type post (without affecting all titles on page)?
<?php
add_filter('the_title', 'new_title', 10, 2);
function new_title($title, $id) {
if('babysitters' == get_post_type($id)){
$gender = get_field('gender');
$dob = get_field('date_of_birth');
$birthday = new DateTime($dob);
$interval = $birthday->diff(new DateTime);
$newtitle = $title .', ' .$gender .', ' .$interval->y;
}
else{
$newtitle = $title;
}
return $newtitle;
}
?>

Magento Sales/Order/Grid change color depending on status

I am overriding on my module the sales grid to get different reports and i was trying to set a different color if order status is "complete" etc.
Here is my approach , it is not giving errors but doesn't seem to work.
class Mycustom_Salesorderitemgrid_Block_Adminhtml_Order_Items_Grid_Renderer_Order
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$value = $row->getData($this->getColumn()->getIndex());
$html ='<a href="' . $this->getUrl('adminhtml/sales_order/view', array('order_id' => $row->getData('order_id'), 'key' => $this->getCacheKey())) . '" target="_blank" title="' . $value . '" >' . $row->getData($this->getColumn()->getIndex()) . '</a>';
return $html;
// here i am trying to add the color to mass status, after finding solution i will add seperate colors based on status
$truncateLength = 255;
// stringLength() is for legacy purposes
if ($this->getColumn()->getStringLimit()) {
$truncateLength = $this->getColumn()->getStringLimit();
}
if ($this->getColumn()->getTruncate()) {
$truncateLength = $this->getColumn()->getTruncate();
}
$text = Mage::helper('core/string')->truncate(parent::_getValue($row), $truncateLength);
if ($this->getColumn()->getEscape()) {
$text = $this->escapeHtml($text);
}
if ($this->getColumn()->getNl2br()) {
$text = nl2br($text);
}
if ($this->getColumn()->getStatusLabel() == array('processing', 'waiting', 'pending', 'almost', 'telephone')) {
$yesterday = strtotime("-24 hours", Mage::getModel('core/date')->gmtTimestamp());
$yesterday = Mage::getModel('core/date')->date(null, $yesterday);
if ($row->getCreatedAt() > $yesterday) {
$text = '<span style="color: red !important; font-weight: bold;">' . $text . '</span>';
};
}
return $text;
}
}
You need to add the method below to your Mage_Adminhtml_Block_Sales_Order_Grid class (or to the class which overrides it):
public function getRowClass($order)
{
if ($order->getStatus() == 'canceled') {
return 'red-row';
}
}
This code will add class="red-row" for every row where the order status is "canceled".
Hope that this will help

How to use array in function to get only value I want

I am trying to work this function in wordpress theme the way where I can get value only what I want and not all to gather. I am not much familiar with using array in function so I need you expert help to make it works and I would really appreciate that.
Here is my code
function gallery_artist($arg=null, $arg2=null, $arg3=null){
global $png_gallery_meta;
$png_gallery_meta->the_meta();
$gallery = get_post_meta(get_the_ID(), $png_gallery_meta->get_the_id(), TRUE);
$gallery_value = $gallery['image_artist'];
$artist_info = $gallery_value;
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
$author_full_name = $author_first_name . ' ' . $author_last_name;
$user_id = '<a href=" ' . get_author_posts_url(basename($string)) . ' " title="more submissions of ' . $author_first_name .' '. $author_last_name . ' " >' . $artist . '</a>';
$arg = $author_first_name;
echo $arg;
$arg2 = $author_last_name;
echo $arg2;
$arg3 = $user_id;
echo $arg3;
}
After than I want to use this function to get any value I want and not unnecessary to render all three to gather. Means if I want only first name than I pass value only for that and it will render only first name so on..
Here how I want to use something. but you can suggest any code and type of function I have no issue.
<?php call_user_func_array('gallery_artist', array('first_name','last_name','artist_id') ) ?>
Big Big thanks to you all..
Try replacing the bottom section with this:
if(!is_null($arg))
{
$arg = $author_first_name;
echo $arg;
}
if(!is_null($arg2))
{
$arg2 = $author_last_name;
echo $arg2;
}
if(!is_null($arg3))
{
$arg3 = $user_id;
echo $arg3;
}

Categories