Wordpress REST API create custom endpoint that uses custom table - php

Is it possible to create a Custom endpoint that is connected to the same database but with a custom table? if yes, how?
for example :
wp_TempTable (Custom Table)
I want to access this with a custom endpoint... I have searched multiple forums and sites but no luck..

Yes it is possible. This does not use the Controller pattern recommended by Wordpress but gets the job done, where the job is to convert incoming json into a row in your custom table (here called restaurants).
function handle_post( WP_REST_Request $request ) {
global $wpdb;
$item = $request->get_json_params();
$fields = array();
$values = array();
foreach($item as $key => $val) {
array_push($fields, preg_replace("/[^A-Za-z0-9]/", '', $key));
array_push($values, $wpdb->prepare('%s', $val));
}
$fields = implode(", ", $fields);
$values = implode(", ", $values);
$query = "INSERT INTO `restaurants` ($fields) VALUES ($values)";
$list = $wpdb->get_results($query);
return $list;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'restos/v1', '/post', array(
'methods' => 'POST',
'callback' => 'handle_post',
'permission_callback' => function () {
return current_user_can( 'edit_others_posts' );
}
) );
} );

Related

Shortcode attributes that query the database

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' );

WordPress, retrieving the ID of the user profile page currently being edited

I'm in the process of developing a WordPress plugin. When editing a user's profile page, I need to be able to retrieve the ID of that user (not the currently logged in user).
I'm using the Advanced Custom fields plugin, and using a load filter for the function in question.
add_filter( 'acf/load_field/name=funbotic_parents', 'funbotic_load_parents' );
function funbotic_load_parents( $field ) {
$args = array(
'role' => 'customer',
'orderby' => 'display_name',
'order' => 'ASC',
);
$parent_data_array = get_users( $args );
// Clear choices array in case it was previously set.
$field['choices'] = array();
foreach ( $parent_data_array as $parent ) {
$parent_ID = $parent->ID;
$parent_display_name = $parent->display_name;
$field['choices'][$parent_ID] = $parent_display_name;
}
$current_user = (int) $_GET['user_id'];
$previously_associated_parents = get_user_meta( $current_user_id, 'funbotic_parents' );
if ( empty( $previously_associated_parents ) || is_null( $previously_associated_parents ) ) {
update_user_meta( $current_user_id, 'funbotic_previously_associated_parents', $previously_associated_parents );
} else {
$new_meta = funbotic_clean_array( $previously_associated_parents );
update_user_meta( $current_user_id, 'funbotic_previously_associated_parents', $new_meta );
}
return $field;
}
The line $current_user = (int) $_GET['user_id']; is returning null. Is there a way to pull 'user_id' from the URL? It's not an ideal solution but at this point I'm starting to get desperate. The URL looks like this:
http://dev-funbotic.local/wp-admin/user-edit.php?user_id=115&wp_http_referer=%2Fwp-admin%2Fusers.php%3Fs%3Dtest%26action%3D-1%26new_role%26course_id%26group_id%26paged%3D1%26action2%3D-1%26new_role2
You can try getting the current url and breaking it down to query params like this:
$query = explode('&', $_SERVER['QUERY_STRING']);
$params = array();
if(!empty($query[0])){
foreach( $query as $param ){
list($name, $value) = explode('=', $param, 2);
$params[urldecode($name)][] = urldecode($value);
}
}
then you can access the user_id by $params['user_id'][0]

Access WP custom REST Fields in custom route

I have created custom fields to display in WordPress rest api. I want the same data to be fetched into a custom route api. How it is possible?
That is, I have some custom rest fields in the path example.com/wp-json/wp/v2/posts/
have the following rest fields I need to show in the another route. ie at example.com/wp-json/mycustom/route/v2
How can I do it?
function list_subpages() {
$data = array();
$request = array();
$my_column = array();
global $wpdb;
$save_table = $wpdb->prefix.'saved_post';
$regid = $_REQUEST['save_key'] ;
$sql1 = "select postid FROM ".$save_table." WHERE regid='".$regid."';";
$result1 = $wpdb->get_results($sql1);
foreach ($result1 as $p)
{
$id[]=$p->postid;
}
$args = array(
'post__in' => $id,
'per_page' => $per_page,
);
$subpages = get_posts( $args );
if ( empty( $subpages ) ) {
return null;
}
foreach ($subpages as $p) {
$request[] = $data;
}
return new WP_REST_Response($request, 200);
}
add_action('rest_api_init', function () {
$namespace = 'savedpost/v2';
$base = 'user';
register_rest_route($namespace, '/' . $base, array(
'methods' => 'GET',
'callback' => 'list_subpages',
));
});

Adding custom meta into wordpress search

I'm currently trying to add my theme posts custom meta values into search query, but it's not working for me. For example _My_meta_value_key3 is product code, but if i try to search it, wp don't find the match. I really want to make this without any plugins so any suggestions are welcome. Also, this code is currently located in theme functions.
function My_custom_search_query( $query ) {
if ($query->is_search()) {
$query->set('meta_query', array(
array(
'key' => '_My_meta_value_key',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
),
array(
'key' => '_My_meta_value_key2',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
),
array(
'key' => '_My_meta_value_key3',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
)
));
return $query;
};}
add_filter( 'pre_get_posts', 'My_custom_search_query');
I've spent an hour searching for a solution and there was nothing that works so I had to write the solution on my own. It is tested and working with Wordpress 5.3.1 and is the only solution that solves this problem I could find on the internet.
Place the following code in your functions.php file to hook into posts_clauses filter.
/**
* Include meta fields in search
*
* #author Mindaugas // meevly.com
* #link https://meevly.com/services/custom-wordpress-themes-and-plugins/
*
* #param array $pieces query pieces.
* #param WP_Query $args query object.
* #return array
*/
function mv_meta_in_search_query( $pieces, $args ) {
global $wpdb;
if ( ! empty( $args->query['s'] ) ) { // only run on search query.
$keywords = explode(' ', get_query_var('s'));
$escaped_percent = $wpdb->placeholder_escape(); // WordPress escapes "%" since 4.8.3 so we can't use percent character directly.
$query = "";
foreach ($keywords as $word) {
$query .= " (unique_postmeta_selector.meta_value LIKE '{$escaped_percent}{$word}{$escaped_percent}') OR ";
}
if ( ! empty( $query ) ) { // append necessary WHERE and JOIN options.
$pieces['where'] = str_replace( "((({$wpdb->posts}.post_title LIKE '{$escaped_percent}", "( {$query} (({$wpdb->posts}.post_title LIKE '{$escaped_percent}", $pieces['where'] );
$pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS unique_postmeta_selector ON ({$wpdb->posts}.ID = unique_postmeta_selector.post_id) ";
}
}
return $pieces;
}
add_filter( 'posts_clauses', 'mv_meta_in_search_query', 20, 2 );
And your query should look like the following. Please note that suppress_filters => false is mandatory! It will not work without it.
$search_posts_array = array(
'suppress_filters' => false,
's' => $keyword,
);
$search_results = get_posts( $search_posts_array );
Please do not hesitate to credit if I saved you tons of time! :)
Found this http://wpdevsnippets.com/extend-search-include-custom-fields-without-plugin/ :
function custom_search_where($pieces) {
// filter to select search query
if (is_search() && !is_admin()) {
global $wpdb;
$custom_fields = array('field1','field2'); // Array for custom meta fields
$keywords = explode(' ', get_query_var('s'));
$query = "";
foreach ($custom_fields as $field) {
foreach ($keywords as $word) {
$query .= "((mypm1.meta_key = '".$field."')";
$query .= " AND (mypm1.meta_value LIKE '%{$word}%')) OR ";
}
}
if (!empty($query)) {
// add to where clause
$pieces['where'] = str_replace("(((wp_posts.post_title LIKE '%", "( {$query} ((wp_posts.post_title LIKE '%", $pieces['where']);
$pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
}
}
return ($pieces);
}
add_filter('posts_clauses', 'custom_search_where', 20, 1);

PHP Wordpress code echo's slug not term?

Does anybody know how I can change the below code for my custom taxonomy (created in the Magic Fields 2 plugin) to solve my problem. I would like it to echo the name of the selected value rather than slug format. e.g. I'd like to echo Client 1 and Client 2 instead of client-1 and client-2 as it currently does.
I'd like to display multiword names with spaces and correct capitalisation e.g. Joe Bloggs Associates not joe-bloggs-associates.
project_statistics_client is the name of the field created in Magic Fields. The Type of the custom field is Term dropdown and populates with the values from my custom Taxonomy called Clients. This field is located inside of a field group that's named project_statistics but I am not sure if the group name influences the code or not?
Also please note that all the above is in a custom post type called Projects.
I have checked plugin help but still not sure:
Here is the code:
<?php $clients = get_field('project_statistics_client');
foreach($clients as $client){
echo '<div>' . $client . '</div>';
} ?>
global $post;
$taxonomy = 'your taxonomy';
$terms = get_the_terms($post->ID, $taxonomy);
if ( is_array($terms) ) {
echo(implode(',', wp_list_pluck($terms, 'name')));
}
Magic Fields 2 only provides a nice GUI to the basic WordPress taxomony feature and therefore all the WordPress taxonomy functions can be used. In particular, get_the_terms() may be used to get the categories of a post. This returns an array of objects (a post may be in multiple categories). Also, you need to extract the 'name' field from the objects.
Okay, so after a little while I remembered that I had pasted some code from a forum post which has allowed me to populate the Term dropdown field with taxonomy/category options. I now think that this is where the problem lies and why it's outputting slug and not name? I have also decided to use categories rather than taxonomy because I would like to filter the loop of posts based on the selected values and from what I read this is easier to achieve with categories. I attach below the code I have added to functions.php because a) others may want to use it with Magic Fields but also b) in the hope somebody might know what I need to change in order to output name instead of slug?
/**
* Custom Taxonomy Dropdown
*/
// initialisation
global $mf_domain;
// class with static properties encapsulating functions for the field type
class term_field extends mf_custom_fields {
public $allow_multiple = TRUE;
public $has_properties = TRUE;
public function _update_description(){
global $mf_domain;
$this->description = __("This field allows to do relations with taxonomie terms",$mf_domain);
}
public function _options(){
global $mf_domain;
// Get the taxonomie as dropdownoption
$select = array();
$tax = get_taxonomies();
foreach($tax as $k => $v){
$select[] = $v;
}
$data = array(
'option' => array(
'term' => array(
'type' => 'select',
'id' => 'term',
'label' => __('related taxonomy: ',$mf_domain),
'name' => 'mf_field[option][term]',
'default' => '',
'options' => $select,
'add_empty' => false,
'description' => '',
'value' => '',
'div_class' => '',
'class' => ''
),
)
);
return $data;
}
public function display_field( $field, $group_index = 1, $field_index = 1 ) {
global $mf_domain;
// If is not required this field be added a None value
$notype = "";
if( !$field['required_field'] ) {
$notype = ( !empty($field['options']['notype']) ) ? $field['options']['notype'] : __( "-- None --" , $mf_domain );
}
$output = '';
// Get the taxonomie as dropdownoption
$select = array();
$tax = get_taxonomies();
foreach($tax as $k => $v){
$select[] = $v;
}
$option_from_term_array = $field['options']['term'];
$options = get_terms($select[$option_from_term_array], array('hide_empty' => false));
$output = '<div class="mf-dropdown-box">';
$value = $field['input_value'];
$output .= sprintf('<select class="dropdown_mf" id="%s" name="%s" >',$field['input_id'],$field['input_name']);
if( $notype != "" ) {
$output .= "<option value=''>$notype</option>";
}
foreach($options as $option) {
$check = ($option->slug == $value) ? 'selected="selected"' : '';
$output .= sprintf('<option value="%s" %s >%s</option>',
esc_attr($option->slug),
$check,
esc_attr($option->name)
);
}
$output .= '</select>';
$output .= '</div>';
return $output;
}
}

Categories