I have a list of favorited user ids in PHP.
I want to create a secondary membersloop within Buddyboss under a new permalink, e.g.
mypage.com/members/favorited
which only shows the given list of user ids.
The inclusion of only the specific favorited user-ids, I have already implemented as a filter to the membersloop, like so:
function membersloop_show_favorites( $querystring = false, $object = false ) {
// Only do this for the members loop.
if ($object != 'members')
return $querystring;
// Test Input, comes from database later on ...
$ids = array(89, 113); //
// Make the array a comma sep list.
$ids = implode( ',', $ids );
$args = wp_parse_args($querystring);
if ( ! empty($args['include']) )
$args['include'] = $args['include'] . ',' . $ids;
else
$args['include'] = $ids;
$querystring = build_query( $args );
return $querystring;
}
add_action( 'bp_ajax_querystring', 'membersloop_show_favorites', 20, 2 );
The only question is how to make a permalink for that customized membersloop.
Related
I'm trying to populate the options available for selection in a dropdown using data from google sheets. I've found this code that is working just fine, but I want the column name to be dynamic and based on the input from another field.
$location_form_id = '21';
add_filter( 'gform_pre_render_'.$location_form_id, 'populate_posts' );
add_filter( 'gform_pre_validation_'.$location_form_id, 'populate_posts' );
add_filter( 'gform_pre_submission_filter_'.$location_form_id, 'populate_posts' );
add_filter( 'gform_admin_pre_render_'.$location_form_id, 'populate_posts' );
function populate_posts( $form ) {
//the select feild id you want the names to load
$field_ID = 'FIELD_ID_HERE';
//your g sheet ID
$gSheet_form_ID = 'SHEET_ID_HERE';
// which column to scan - what is the heading name
$column_name = 'COLUMN_HEADING_NAME_HERE';
$placeholder = 'YOUR_PLACEHOLDER_HERE';
$list_number = '1';
//get data
$url = 'https://spreadsheets.google.com/feeds/list/'.$gSheet_form_ID.'/'.$list_number.'/public/values?alt=json';
$file = file_get_contents($url);
$json = json_decode($file);
$rows = $json->{'feed'}->{'entry'};
//get all the same from sheet
$names = array(); //store names in this array
foreach($rows as $row) {
$name = $row->{'gsx$'.$column_name}->{'$t'};
array_push($names, $name); //push data
}
//Go through each form fields
foreach ( $form['fields'] as $field ) {
//check if field type is a select dropdown and id is correct
if ( $field->type == 'select' && $field->id == $field_ID) {
//add name and value to the option
foreach($names as $single_name){
$choices[] = array('text' => $single_name, 'value' => $single_name );
}
//Add a place holder
$field->$placeholder;
//Add the new names to the form choices
$field->choices = $choices;
// Print out the contents of the array (troubleshooting only)
//echo '<pre>'; print_r($choices); echo '</pre>';
}
}
return $form; //return form
}
How can I edit this code so that $column_name = whatever input is in Field 1 to show different selections in Field 2 from a google sheets column.
I am trying to create a shortcode that returns a list of doctors matching a specialty.
So far, I can get the base shortcode to return the contents of the entire table, but I can't get it to query based on an attribute string.
Here's what I have:
// Add Shortcode
function list_docs( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'specialty' => '',
),
$atts,
'doctors'
);
global $wpdb;
$specialty = $atts['specialty'];
$specget = $wpdb->prepare("SELECT * FROM doctors WHERE specialty = '%s'", $specialty);
$specresults = $wpdb->get_results($specget);
foreach($specresults as $details) {
echo $details;
}
}
add_shortcode( 'doctors', 'list_docs' );
If I query the database directly with:
SELECT * FROM `doctors` WHERE `specialty` = 'cardiology'
I get the expected result.
I'm trying to call it with [doctors specialty="cardiology"] (I've tried double and single quotes) on the WordPress page.
Right now, I don't know what I don't know. I'm not sure if I've entered something wrong, have a typo, or am missing a line of code. Any assistance would be terrific.
May be problem is not with the query at all assuming that your table name is indeed doctors( & not wp_doctors or something like that )
$specresults will contain an array of objects. Let's say your doctors table has name column, then below changes may work for you.
function list_docs( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'specialty' => '',
),
$atts,
'doctors'
);
global $wpdb;
$specialty = $atts['specialty'];
$specget = $wpdb->prepare( 'SELECT * FROM doctors WHERE specialty = %s', $specialty );
$specresults = $wpdb->get_results( $specget );
if ( $specresults ) {
$doctor_names = array_map(
function( $doctor_object ) {
return $doctor_object->name;
},
$specresults
);
return implode( ', ', $doctor_names );
}
return '';
}
add_shortcode( 'doctors', 'list_docs' );
Couple of things to keep in mind:-
Shortcodes should always return & not echo directly.
Query only for required data from the database as far as possible instead of doing *
If you are going to need only one column, prefer using get_col method on $wpdb instead of get_results.
Please try with this -
function list_docs( $atts ) {
global $wpdb;
$specialty = $atts['specialty'];
$specget = $wpdb->prepare("SELECT * FROM doctors WHERE specialty = %s", $specialty);
$specresults = $wpdb->get_results($specget);
foreach($specresults as $details) {
echo $details;
}
}
add_shortcode( 'doctors', 'list_docs' );
I'm currently trying to modify the name column in the WordPress admin dashboard. I've tried this code here but it's not working:
add_action('manage_users_custom_column', 'modify_users_column_content', 10, 3 );
function modify_users_column_content( $value, $column_name, $user_id ) {
if ( $column_name === 'name' ) {
$value .= '<span> |</span>';
}
return $value;
}
When I error_log the column_name parameter, I get only the last two columns from the user management plugin UltimateMember:
The first columns are not within the array. I've tried to understand it but no chance. I don't get it.
The first columns are not within the array. I've tried to understand
it but no chance. I don't get it.
Because the manage_users_custom_column filter is meant to be used to generate the output of a custom column and not default columns like the "Name" column.
However, you can achieve what you want by replacing the default "Name" column (keyed name) with a custom one like so:
add_filter( 'manage_users_columns', function( $columns ){
$columns2 = [];
// We could do $columns['name2'] = 'Name'; - but we are replacing a column.
foreach ( $columns as $key => $label ) {
if ( 'name' === $key )
$columns2['name2'] = 'Name';
else
$columns2[ $key ] = $label;
}
return $columns2;
} );
And then use the manage_users_custom_column filter to generate the output which is displayed in the custom column (name2):
add_filter( 'manage_users_custom_column', function( $output, $column_name, $user_id ){
if ( 'name2' === $column_name ) {
$user_object = get_userdata( $user_id );
$name = trim( $user_object->first_name . ' ' . $user_object->last_name );
$output = $name ? $name . '<span> |</span>' : '—'; // the custom output
}
return $output;
}, 10, 3 );
I would like to change the order received (thanks page) url for a new one, which includes the ordered product's ID.
Here is the code in the class-wc-order.php
/**
* Generates a URL for the thanks page (order received).
*
* #return string
*/
public function get_checkout_order_received_url() {
$order_received_url = wc_get_endpoint_url( 'order-received', $this->get_id(), wc_get_page_permalink( 'checkout' ));
if ( 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) || is_ssl() ) {
$order_received_url = str_replace( 'http:', 'https:', $order_received_url );
}
$order_received_url = add_query_arg( 'key', $this->get_order_key(), $order_received_url );
return apply_filters( 'woocommerce_get_checkout_order_received_url', $order_received_url, $product, $this, $product->id );
}
Any idea?
Thanks!
This answer on wordpress.stackexchange.com would solve the issue
As a brief, just define the following hook:
add_filter('woocommerce_get_checkout_order_received_url','override_return_url',10,2);
function override_return_url($return_url,$order){
//create empty array to store url parameters in
$sku_list = array();
// retrive products in order
foreach($order->get_items() as $key => $item)
{
$product = wc_get_product($item['product_id']);
//get sku of each product and insert it in array
$sku_list['product_'.$item['product_id'] . 'sku'] = $product->get_sku();
}
//build query strings out of the SKU array
$url_extension = http_build_query($sku_list);
//append our strings to original url
$modified_url = $return_url.'&'.$url_extension;
return $modified_url;
}
Then, you can set the analytics goal to point to the destination below as a regular expression
\/checkout\/order-received\/\d+\/\?key=\w+&product_ID_HERE_sku=SKU_HERE
We can make it simpler by including only product ids:
add_filter('woocommerce_get_checkout_order_received_url','override_return_url',0,2);
function override_return_url($return_url,$order){
$ids = array();
foreach($order->get_items() as $key => $item)
{
$ids[] = $item['product_id'];
}
return add_query_arg('product_ids', join(',', $ids) , $return_url);
}
In this case, the regex would be:
\/checkout\/order-received\/\d+\/\?key=\w+&product_ids=ID1,ID2,ID3
Or for a single product id:
\/checkout\/order-received\/\d+\/\?key=\w+&product_ids=[\w,]*ID[\w,]*
I'm not good with PHP.
I use "Advanced Categories Widget" to list categories on Sidebar.
I used this plugin because it offers the ability to display images categories.
But I need to order categories by random.
I find this code on the plugin:
function advanced_categories_widget_html( $args = array() ) {
$args = wp_parse_args( $args );
$args['walker'] = new Walker_Advance_Category_Widget;
$output = wp_list_categories( $args );
if ( $output ) return $output;
}
and i Find another code on forum which displays correctly categories by random order:
wp_list_categories
how I can exploit the second code to hack the first code to list my categories with random order?
The PHP file for the plugin: http://codepad.org/a3yU7Xny
Accordion to the documentation on the Advanced Categories Widget plugin you're using -- -- you can specify 'orderby' in your plugin settings. See this screenshot.
You should have a random or rand option in the drop down.
Can't confirm cuz it's a paid plugin.
Just add the "hack" the function in the plugin file :
function advanced_categories_widget_html( $args = array() ) {
$args = wp_parse_args( $args );
$args['walker'] = new Walker_Advance_Category_Widget;
$cats ='';
$categories=get_categories();
$rand_keys = array_rand($categories, 5); // 5 is the number of categories you want
foreach ($rand_keys as $key) {
$cats .= $categories[$key]->term_id .',';
}
$output = wp_list_categories($args.'&include='.$cats);
if ( $output ) return $output;
}
Or by a cleaner way, add in your functions.php file :
function random_advanced_categories_widget_html( $args = array() ) {
$args = wp_parse_args( $args );
$args['walker'] = new Walker_Advance_Category_Widget;
$cats ='';
$categories=get_categories();
$rand_keys = array_rand($categories, 5); // 5 is the number of categories you want
foreach ($rand_keys as $key) {
$cats .= $categories[$key]->term_id .',';
}
$output = wp_list_categories($args.'&include='.$cats);
if ( $output ) return $output;
}