Last active
March 6, 2018 11:42
-
-
Save daggerhart/10417309 to your computer and use it in GitHub Desktop.
An example of creating a custom field for display with Query Wrangler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Custom QW Field Example | |
Description: Example of custom Query Wrangler field with $post and $field parameters. | |
Author: Jonathan Daggerhart | |
Version: 1.0 | |
*/ | |
add_filter('qw_fields', 'custom_qw_field'); | |
function custom_qw_field($fields) | |
{ | |
$fields['custom_qw_field'] = array( | |
'title' => 'Custom QW Field', | |
'description' => 'Just an example of making your own custom fields with callbacks.', | |
'output_callback' => 'get_custom_qw_field', | |
'output_arguments' => true, // this provides the $post and $field parameters to the output_callback | |
); | |
return $fields; | |
} | |
/* | |
* This is just a random custom function that returns some html. | |
* | |
* @param object $this_post - the WP post object | |
* @param array $field - the QW field. Useful if a field has custom settings (not shown in this example). | |
* @param array $tokens - Available output values from other fields | |
*/ | |
function get_custom_qw_field($this_post, $field, $tokens){ | |
// since this field is executed with "the loop", you can use most WP "template tags" as normal | |
// you can do anything you need here, as long as you return (not echo) the HTML you want to display. | |
$author = get_the_author(); | |
// this would provide the same results | |
// $author = get_the_author_meta('display_name', $this_post->post_author); | |
return "This post is authored by: ".$author; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just in case anyone is as lost as I was a little while ago, I used this snippet to create my own custom field, just placed it in my theme's functions.php file and it worked great! But I'm not really sure that was the best spot for it. Should it go in a plugin or use some hook to make sure it's called in time? Anyways, what a great plugin!