How can I create custom endpoint with post and get methods on wordpress api - wordpress

I'm trying to make a custom endpoint, for example:
https://localhostname.com/wp-json/company_name/settings
Where I'll keep some settings like theme colors and other things, but it seems there are many ways to do that and I just want the simplest way. All the searching on the web is making me really confused.
So basically I want to make a POST request to the above URL like this:
{
"primary_color": "0xFFFFFFFF",
"secondary_color": "0xFFFFFF11"
}
and then get these same parameters in a GET request.
It's just that. No verification at all. Sorry if it's that simple, but I'm really having a hard time with this problem as I am not used to program with php at all.

To create a custom endpoint you will need to add below snippet to your function file:
add_action( 'init', 'setup_init' );
function setup_init() {
add_action( 'rest_api_init', 'custom_endpoint' );
function custom_endpoint() {
register_rest_route( 'company_name', '/settings', array(
'methods' => 'GET',
'callback' => 'custom_callback',
));
}
function custom_callback($request_data){
return 'hello world';
}
}
Let me know if any query and do accept the answer if it works :)

Related

WordPress publish_{$post_type} hook works only for posts and not for custom post types or pages

I'm trying to send push notification when any of posts, custom post types or pages is published. I'm getting enabled post types from the plugin settings and adding action via foreach loop in my class __construct method. The problem is that it only works for posts and not for any of custom post types or pages. Here is my function and action:
foreach ((array)get_option('PushPostTypes') as $postType) {
add_action("publish_{$postType}", array($this, 'doNewPostPush'), 10, 2);
}
public function doNewPostPush($id, $post) {
$pushData = array(
'title' => $post->post_title,
'body' => strip_tags($post->post_content),
'data' => array(
'url' => trailingslashit(get_permalink($id)),
),
);
if (has_post_thumbnail($id)) {
$pushData['image'] = get_the_post_thumbnail_url($id);
}
$this->sendNotification($pushData);
}
get_option('PushPostTypes') is an array of post types that user choose, for example: array('post', 'page', 'custom_post');
Any idea why it only works for post and not for pages or custom post types?
Your code worked for me, assuming your get_option('PushPostTypes') is working as intended (Obviously I had to mock that).
Try a different approach that does not rely on get_option('PushPostTypes') to see if you get the same result;
add_action('transition_post_status', function ($new_status, $old_status, $post) {
if ($new_status !== 'publish') {
return;
}
// do something
}, 10, 3);
Try the 'transition_post_status' hook that works for all posts without specifically defining them. Put that somewhere just to see if it runs. Do whatever debugging statement that suits you. Then if that works, then move it into Class code and see if that works. I'm debugging by trying to isolate where the first thing goes wrong. Keeping it very simple to get it to work, then gradually adding complexity until it breaks.

Wordpress / Woocommerce Hook into a Function

I appreciate questions similar to this have been asked before but the answers I've tried aren't doing what I need.
Basically,
I have this file in the woocommerce plugin folder structure:
wp-content\plugins\woocommerce\includes\wc-coupon-functions.php
Inside the file is the following function:
function wc_get_cart_coupon_types() {
return (array) apply_filters( 'woocommerce_cart_coupon_types', array( 'fixed_cart') );
}
I need to override it so it returns an additional item in the array but nothing I've tried has worked. I've tried:
Creating the same file in my custom theme file
hooking the function in my functions file with the following code:
function woocommerce_coupon_get_cart_coupon_types()
{
return (array) apply_filters( 'woocommerce_cart_coupon_types', array( 'fixed_cart', 'custom_discount' ) );
}
add_filter('wc_get_cart_coupon_types', 'woocommerce_coupon_get_cart_coupon_types',10, 1);
Any other suggestions would be greatly appreciated, also..... I've made the change directly in the file and it definitely works. Thanks
Your #2 approach is sort of how to do it, but you're essentially caught in a loop situation the way you did it.
You need to do it this way:
function wpso59974749_woocommerce_coupon_get_cart_coupon_types( $data ) {
$data[ 'your_new_key_here' ] = 'your new value here';
return $data;
}
add_filter('woocommerce_cart_coupon_types','wpso59974749_woocommerce_coupon_get_cart_coupon_types',10, 1);
You shouldn't add the apply_filter back in your function, as it would get stuck in a loop - essentially refiltering itself over and over.
I prefixed your function so if there is another woocommerce_coupon_get_cart_coupon_types function, it won't conflict.

plugin add action hook is not working :

I am trying to add an custom action hook in wordpress but it's not working.Please help me through this.
<?php
function wp_add_google_link(){
global $WP_Admin_Bar;
var_dump($WP_Admin_Bar);
$WP_Admin_Bar->add_menu(array(
'id'=>'google_analytics',
'title'=>'GoogleAnalytics',
'href'=>'https://google.com/analytics'
));
}
add_action('wp_before_admin_bar_render','wp_add_google_link');
I guess it is working but returning NULL value with an error. It is because wp_admin_bar should be lower case. But you are using WP_Admin_Bar.
Please check the reference:
https://codex.wordpress.org/Class_Reference/WP_Admin_Bar
and
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_before_admin_bar_render
function wp_add_google_link(){
global $wp_admin_bar;
var_dump($wp_admin_bar);
$wp_admin_bar->add_menu(array(
'id'=>'google_analytics',
'title'=>'GoogleAnalytics',
'href'=>'https://google.com/analytics'
));
}
add_action('wp_before_admin_bar_render','wp_add_google_link');
Screenshot of result

Wordpress Plugin: Show html only on standard page and not in admin area

I'm writing a plugin and I need to display a piece of text in the WP page, but not in the admin area. How can I do so?
I tried this in the construct:
add_action( 'init', array( $this, 'initPage' ) )
and then:
public function initPage() {
echo 'hello';
}
but the text is displayed also in the admin area. Is there a way to do this? It would be the opposite of the action admin_init I assume.
Proper way to handle it: is_admin()
http://codex.wordpress.org/Function_Reference/is_admin
if(is_admin()) { // do nothing } else {
// function you want to execute.
}
I solved this by adding it to a shortcode action. Like this:
add_shortcode( 'myPlugin', array( $this, 'shortcode' ) );
and:
public function shortcode( $atts ) {
return 'hello';
}
With the above code, 'hello' will only display on the front-end. Not sure if that's the cleaner way to do it, but does the job.
There is no "front-end-only" version of init, however you probably don't want to be doing any output at the init action anyway.
What exactly are you trying to do? Usually, you use an action hook for specific types of things, and causing output very early at something like "init" is rare and weird.

Wordpress - using php to redirect to a different page

I am trying to create a form that when submitted, adds data to a mysql database and redirects to a success page.
I added this code to the bottom of my functions.php file:
add_action('init', 'form_submit');
function form_submit(){
global $wpdb;
if(isset($_POST['form_sub']))
{
$name= $_POST['name'];
$age= $_POST['age'];
$wpdb->insert(
'mytable',
array(
'name' => $name,
'age' => $age
),
array(
'%s',
'%s'
)
);
header("Location: http://www.google.com");
}
}
and added a form to my page that calls this function when submit is clicked.
When I try this, the data is added to the mysql database, but the redirect doesn't seem to work. Instead of going to google.com, it adds this to the top of the page:
Object Moved
This document may be found here
Where here is a link to google.com. Any idea on how to fix this? Thank you!
You are probably sending the redirect header too late (after the page is rendered).
Check this out: http://shibashake.com/wordpress-theme/wordpress-page-redirect
You can try to put out a javascript redirect if you can't get the header() or wp_redirect() out early enough.

Resources