I have an entire post notification system built using firebase. Using a program like postman, I can send the title & body and the notifications show up on the iOS/android devices. So the entire linkage works. Specifically in postman using the Post function, the URL posted too is https://example.com/backend/sendNotification.php. The body code is:
{
"title" : "post title",
"body" : "post body"
}
But now I have to tie it into wordpress. The goal is that when a post is published, the title and body, most likely truncated to a limited amounts of characters, get passed to the URL.
I know the code will go in the function.php file, but beyond that I'm stumped. How can I pass the title & body to the URL?
First, you want to hook into the post creation. There are several hooks that would probably fit your needs. Then use wp_remote_post or curl to post your data to the webhook.
add_filter( 'wp_insert_post_data', 'push_post_data' );
function push_post_data( $data ) {
if ('publish' == $data['post_status']) {
wp_remote_post($url, $array_with_title_and body);
}
}
If you're using a child theme or a custom theme functions.php is fine.
Related
Using the Avada theme's form builder functionality, I’m attempting to use the “Send to URL” option to POST the form and then run an api call in a plugin I’ve written.
I was thinking I could set the “Send to URL” value to /wp-admin/admin-post.php and add a hidden field to the form named “action” with a value of “make_api_call” to get it to run the code in my plugin set up like so:
add_action('admin_post_make_api_call', 'make_api_call');
function make_api_call()
{
//todo: make the api call
wp_redirect(‘another-page’);
}
However, this does not work and returns this from the server: {"status":"error","info":"url_failed"}
What I want to do is to POST the form to admin-post.php, have my plugin run code when it POSTs, and then redirect to another page.
I've checked the documentation for the AVADA theme and it only says that you can specify a url to post to but doesn't give any additional details.
So I eventually got something to work, and I'm not knowledgeable or experienced enough with WordPress development to know if it is the "right" way, but it works well.
I realized that using the "Send to Url" option on the Avada form, it was POSTing the form to the admin-ajax.php file. There's plenty of documentation on that and I was able to partially make that work but I was not able to make it fit my use case b/c even though there is a way to configure the Avada form to redirect to a different URL on success I couldn't append parameters to that URL based on the return value from admin-ajax.php.
For future reference, here's what I was able to make work but not fit my use case by having the Avada form submission set to Send to Url. (I'm recreating this and some of it's from memory since I went with a different solution, so it may not be 100% runnable.)
The way admin-ajax works is all requests to admin-ajax.php are eventually handled by a WordPress action (filter?) like so:
add_action( 'wp_ajax_my_action', 'my_function' );
In the above, my_action is what you've set as the form's action by creating a hidden input element on your html form named action and setting it's value to "my_action". The my_function argument is the name of the function you want to run when the action happens.
add_action( 'wp_ajax_my_action', 'my_function' );
function my_function(){
//do stuff
}
Watching the request in Chrome's dev tools, I could see the action the form was setting was fusion_form_submit_form_to_url.
So ended up with this:
add_action( 'wp_ajax_fusion_form_submit_form_to_url', 'my_function' );
function my_function(){
//do stuff
}
You can see that the url you enter in the Form Submission URL field gets passed to admin-ajax as fusionAction. Whether the Avada theme does something additional with that - I don't know but you could use it to control the logic that gets executed in my_function. I suspect there's an action in the Avada form that works similar to the wp_ajax_ WordPress action but by the time I got this far I realized this wasn't going to work so I pivoted to the actual solution, below.
All of that worked okay but you can't redirect out of a call to admin-ajax.php unless you do it on the client side and I didn't want to dive into that.
What I was able to make work was configuring the Avada form to do a traditional HTTP POST. I added a hidden input element on the Avada form with a name of formName and the value set to the name of the form I wanted to handle.
In my plugin code, I hooked into the WordPress init action as in the code sample below, and then customized the logic to be executed based on which formName was sent in.
add_action('init', 'callback_function');
function callback_function()
{
if (isset($_POST['formName'])) {
$form = $_POST['formName']; //from the hidden input element "formName"
//there is a call to wp_redirect in each case
switch ($form) {
case 'form1':
process_form_1();
break;
case 'form2':
process_form_1();
break;
default:
# code...
break;
}
//without this "exit" you will get errors similar to "headers already sent"
exit;
}
}
This allowed me to run the code I needed to run based on what form was submitted, and redirect to the correct place afterward.
I would want to show some HTML code in my WordPress page. So in my child theme functions.php, I have written an action like this one:
add_action('wp', function() {
if(is_admin()) {
return;
}
$html = '<div class="home_immodvisor">';
echo $html;
});
However, a warning is displayed when I go to the back-office (admin) login page:
Warning: Cannot modify header information - headers already sent by
(output started at
/var/www/html/wp-content/themes/hello-elementor-child/functions.php:88)
in /var/www/html/wp-includes/pluggable.php on line 1296
Warning: Cannot modify header information - headers already sent by
(output started at
/var/www/html/wp-content/themes/hello-elementor-child/functions.php:88)
in /var/www/html/wp-includes/pluggable.php on line 1299
My question is: is it the good action to hook? Or should I use another one than wp?
From my understanding wordpress sends headers after wp so this is the wrong place, since you cannot echo before.
There is no standardized hook/action for inserting a html in a theme. Every theme could handle this differently, some might not even have a hook.
But if you must the wp_head hook will work, but it will not be valid HTML.
The better thing todo is check the documentation of your thema. Or make a child-theme and adjust the tempaltes) you need
Warning: Cannot modify header information - headers already sent by
To solve this add the below code in theme function.php file
ob_start();
It will remove your error.
Having an issue where when using the following payload with a post request on https://localhost:2368/ghost/api/v0.1/posts the body of the post is not loaded into ghost blog.
{
title: 'some title',
html: '<p>some html</p>',
plaintext: 'some plaintext'
}
After the request, only the title is loaded into ghost blog.
Using Ghost v2.13.1
Would be helpful discover field(s) that should be used for the body of a post.
#mattcameron Ghost uses mobiledoc (spec) - the html and plaintext fields are generated from that and can't be set separately.
The v0.1 API is private and not documented as it wasn't designed for use outside of the admin area, if you can I suggest you wait a little while for the v2 Admin API which will be released soon along with SDKs to make it easier to work with.
i was looking for a way to include some plugin or something like that in my wordpress site. This "plugin" send a mail (to specific contacts) every time i create a new content in my website (event, article, etc..). do you guys know a way to do that? Thanks a lot!
Recommend to solve this with the publish post action hook - mail to all recipients will be sent on publishing a post. This is how you can achieve it:
add_action('publish_post','notify_my_buddies');
function notify_my_buddies($ID, $post ) {
$recipients = get_option('my_recipients');
foreach($recipients as $rec) {
wp_mail($rec,"New Post: ".$post->post_title, "Any content pass here ...");
}
}
I want to show my wordpress post into jquery mobile application... But so far i didnt got the success. I am using jquery.post() function but my response comes empty....
Request to the desired url goes well , status comes 200 ok but response coming is always blank :( Although the same post function & url is working fine in other php pages....
below is my code
function get_Time(cityCode,date){
jQuery.post(
"http://test.local/time/",
{ city: cityCode, date:date},
function (data){jQuery('#print_time').html(data);}
);
}
function _get_Time(response)
{
alert("response:"+response);
var time = new Array();
time = response.split('|')
jQuery("#print_time").html(time[0]);
}
Please give me some solution for showing my wordpress post (only text + links) content into my jquery mobile applicaton....
I am not sure about what you are going to do. It seems to me, like you want to retrieve a certain WordPress post content + additional information via Ajax, I think the easiest way to do it is the following:
Write a custom server-side PHP-Script (which possibly takes a WordPress Slug or ID)
Create a connection to the database in this script, get the post contents you want to have and return these as text or JSON to your JQuery mobile and use it
It might work in a way like this (not tested):
include ‘path-to-wp-directory/wp-blog-header.php’;
$post_id=0;
if(isset($_REQUEST['post_id'])) {
$post_id=intval($_REQUEST['post_id']);
}
global $wpdb;
$post_content = $wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE post_id=".$post_id." AND post_status='publish'";
echo $post_content;
This is only possible if you have access to the server, of course. If you want to get Post Content from an external WordPress Blog, then you should consider using the RSS Feed.
The following article shows how to load WordPress functions in an external PHP Script, which also might be useful: http://www.bloggingtips.com/2009/01/25/use-wordpress-functions-externally/