Search posts by custom fields using xml-rpc - wordpress

Is it possible? Or is there any other way to do it without xml-rpc?

Yes, it is possible, but you have to write your own xml-rpc method.
For example:
add_filter( 'xmlrpc_methods', 'my_add_xmlrpc_methods' );
//this function adds new xml-rpc method
//my.search_posts - name of xml-rpc method
//search_posts_by_custom_field - name of the function that should proccess my.search_posts method call
function my_add_xmlrpc_methods( $methods ) {
$methods['my.search_posts'] = 'search_posts_by_custom_field';
return $methods;
}
//this function actually search posts
function search_posts_by_custom_field($args) {
//authenticate user
//run search DB query
//return results
}

Related

Wordpress - Build one Page to use URL to pull data from REST API

I'd appreciate any advice, resources or assistance with this issue.
I want to be able to have part of my Wordpress site where I can parse the URL and then use those values to populate the page with content from another API.
For example:
server.zz/weather/Sydney%20Australia
server.zz/weather/Houston%20Texas
Where I could write a Plugin which would intercept these requests, be able to extract the end of the URLs, and then call another API to get the data to then merge into a Template to be presented to the visitor.
I know that there are Custom Post Types, but I wasn't sure if they were the best solution for this usage case.
As I said, any advice or suggestions would be appreciated.
I found the solution to this problem by using add_rewrite_rule(), add_rewrite_endpoint(), and flush_rewrite_rule().
For the example I provided earlier, I created the following code in a Plugin.
// Define the URL Rewrite Rules
function crw_rewrite_urls(){
add_rewrite_rule(
'^weather/(.+)$' ,
'index.php?weather_location=$matches[1]' ,
'top'
);
add_rewrite_endpoint('weather_location', EP_ROOT);
flush_rewrite_rules();
}
add_action('init', 'crw_rewrite_urls');
// Initialise the Query Variable
function crw_query_var( $vars ) {
$vars[] = 'weather_location';
return $vars;
}
// Check for the Variable and Display Content as needed
function crw_handler() {
global $wp_query;
if ( isset( $wp_query->query_vars['weather_location'] ) ) {
// Call the API, fill the Template here
}
return;
}
add_action('template_redirect', 'crw_handler');

woocommerce_email_recipient_customer_completed_order hook not being invoked

There are a bunch of example of using this woocommerce hook woocommerce_email_recipient_customer_completed_order. So I added it to functions.php, and concatenated a couple example recipients as a test, but only the purchaser receives an email. It doesn't seem like this filter is getting invoked since if I turn on debugging and error_log(...) there's nothing in the log file.
Is there a reason this isn't working? I tried bumping the priority up to 99, but that doesn't work either. The site uses all the defaults and doesn't override any of the templates.
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'custom_woocommerce_add_email_recipient', 10, 2);
function custom_woocommerce_add_email_recipient($recipient, $order) {
$recipient = $recipient . ', foo#example.com, bar#example.com';
return $recipient;
}
As I learn more about Woocommerce it looks like this site only goes as far as processing so I had to use the customer_processing_order mail ID instead to get this to work (thanks to #LoicTheAztec for verifying the customer_completed_order hook works), and instead of using the woocommerce_email_recipient_customer_processing_order hook I used woocommerce_email_headers and checked the mail ID.
add_filter( 'woocommerce_email_headers', 'woocommerce_email_headers_add_recipient', 10, 3);
function woocommerce_email_headers_add_recipient($headers, $mail_id, $order) {
if($mail_id == 'customer_processing_order') {
$member = null;
$memberMail = null;
foreach($order->get_meta_data() as $item) {
if ($item->key === 'member_name') {
$member = $item->value;
$memberMail = $this->getMemberMail($member);
$headers .= "BCC: {$member} <{$member_mail}>\r\n";
break;
}
}
}
return $headers;
}

Drupal custom form twice on same page, i need the errors displayed in the one submitted and not on both

I've a custom module that builds a form with a couple of fields, so far so good.
In one of my pages, i print this form twice (different blocks), the form gets the same "form_id", so when i submit one of them and get an error, both of them get the error highlighted, and the fields populated.
I want that only the form i submit gets the errors, is there a way to do this?
Thanks!!
For anyone interested, to do this you need to use the hook_forms.
This hook only gets called when the form_id passed to a drupal_get_form doesn't exist, this is important, if you want to use this, make sure your calls use a non existing form_id, for example:
//Defining the form:
function mx_wtransnet_form_contacto($form, &$form_state, $block = null, $formType = null) {
}
I want to use this form multiple times and get different error handlers, instead of loading my form (mx_transnet_form_contacto), i'll call a non existing one:
$form = drupal_get_form("mx_wtransnet_form_contacto_invalid", "contacto-mini");
Then i create my hook:
function mx_wtransnet_forms($form_id, $args) {
$forms = array();
if (strpos($form_id, '_contacto_') !==false) {
$forms[$form_id] = array(
'callback' => 'mx_wtransnet_form_contacto',
);
}
return $forms;
}
This function will catch all my druapl_get_form calls that don't exist, so i can process/direct them, in my example, what i do is simply check that the form_id contains contacto and then set the callback for this form to the original function.
In this case better to create another form with different "form_id" but with the same submit handler.
Another case: when you output same form twice on the page it also may get JS errors because ID of form elements are the same.
In case you don't repeat the form code and its submit handler(DRY principle), I would recommend create a custom function that has the form array
function form_my_custom($form_id){
$form['my_first_field'] = array();
$form['my_second_field'] = array();
$form['#attributes']['id'] = $form_id;
$form['my_submit_button'] = array(
'#submit' => array('my_custom_form_submit')
);
return $form;
}
function my_block1_form(){
return my_custom_form('my_form_id_1');
}
function my_block2_form(){
return my_custom_form('my_form_id_2');
}
function my_custom_form_submit(&$form, &$form_state){
// your submit handler.
}

In Drupal 6 What is the hook I would use to override user_pas_submit?

I need some additional functionality added to user_pass_submit. This must be added without changing the core. What is the hook I would use to do this?
Thanks.
You should be able to use hook_form_FORM_ID_alter() to replace the submit handler for the user_pass form with your own module's copy of user_pass_submit, altered as necessary.
Something like this...
function MODULE_form_user_pass_alter(&$form, &$form_state) {
$form['#submit'] = array('MODULE_user_pass_submit');
}
function MODULE_user_pass_submit($form, &$form_state) {
global $language;
$account = $form_state['values']['account'];
// Mail one time login URL and instructions using current language.
_user_mail_notify('password_reset', $account, $language);
watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
$form_state['redirect'] = 'user';
return;
}
Of course, this falls down if you have multiple modules trying to do

Wordpress: Accesing post title from content_save_pre

i modify content when saving and need to access post title, what i have so far in functions.php
add_filter('content_save_pre', 'custom_content_save_pre');
function custom_content_save_pre($s) {
// need to access post title here
$postTitle = ?
// some more code here
return $s;
}
Globalize the $post object and you will have access to it ex:
add_filter('content_save_pre', 'custom_content_save_pre');
function custom_content_save_pre($s) {
global $post;
$postTitle = $post->title;
return $s;
}
but a better hook to use would be wp_insert_post_data which accepts 2 parameters $data and $postarr
where data is an array of the post object that is needed to be returned for saving.

Resources