I am trying to create a wordpress plugin to create a custom URL. So that when that URL run in browser my PHP code should run.
I am having the below code in my plugin
add_action( 'rest_api_init', 'my_register_route' );
function my_register_route() {
register_rest_route( 'my-route', 'my-phrase', array(
'method' => 'GET',
'callback' => 'custom_phrase',
)
);
}
function custom_phrase() {
return rest_ensure_response( 'Hello World! This is my first REST API' );
}
My plugin got activated in admin side.
But when enter the custom path in browser, its not working
http://localhost/wp/wp-json/my-route/my-phrase
it shows 404 not found.
Please help to find where i have error.
After activating the plugin. Go to WordPress Settings -> Permalinks. Click on save permalinks buttons once. Now the new route will start working.
Related
I'm trying to get a list of taxonomies using the WordPress REST API. Hitting /wp-json/wp/v2/taxonomies/post_tag works fine, but I also have a custom taxonomy called location and accessing /wp-json/wp/v2/taxonomies/location returns a 403 rest_forbidden error.
I can't figure out under what circumstances taxonomy REST access would be forbidden in this way. Any ideas?
You need to set show_in_rest to true when registering your taxonomy.
https://codex.wordpress.org/Function_Reference/register_taxonomy
If your custom taxonomy was created by a plugin and you need to alter it's behaviour try this post :
http://scottbolinger.com/custom-taxonomies-in-the-wp-api/
In short, you can add the code below to your functions file to enable show_in_rest for all custom taxonomies.
function prefix_add_taxonimies_to_api() {
$args = array(
'public' => true,
'_builtin' => false
);
$taxonomies = get_taxonomies($args, 'objects');
foreach($taxonomies as $taxonomy) {
$taxonomy->show_in_rest = true;
}
}
add_action('init', 'prefix_add_taxonimies_to_api', 30);
I hope this helps you.
Elementor is generating page specific styling and scripts inside head section of the WordPress page. Is there a way to read them to GraphQL?
I did the same for WpBakery pagebuilder in 2 steps:
register new field in graphQL with my CSS from editor, something like:
add_action( 'graphql_register_types', function() {
register_graphql_field( 'Page', 'customCSS', [
'type' => 'String',
'description' => __( 'Custom CSS from Editor', 'wp-graphql' ),
'resolve' => function() {
// call your function which return your custom css
return $this->addPageCustomCssToGraphql();
}
] );
} );
request them in Gatsby page and using Helmet plugin show your CSS in HEAD of your page.
I am trying to figure out how to create a Wordpress plugin that adds a page to the Wordpress website on which it is installed.
I came up with the following which works and adds the page.
However, it's far from what I am trying to achieve:
How to change the entire page? Not just the content? Right now, it has all the header and footer and navbar.
How to have PHP code in the page, not just static content?
Is it possible to have everything under a url (https://some-url.com/my-plugin/) routed to this same page?
For example:
https://some-url.com/my-plugin/ -> run my page
https://some-url.com/my-plugin/foo/ -> run my page
https://some-url.com/my-plugin/foo2/abc/ -> run my page
etc.
<?php
/**
* Plugin Name: MyPlugin
* Plugin URI: myplugin.com
* Description: MyPlugin
* Version: 1.0
* Author: Mike
* Author URI: myplugin.com
*/
define( 'MYPLUGIN__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
function create_page() {
$post_data = array(
'post_title' => 'Test of my plugin',
# How to change the entire page? Not just the content?
# Also, how to have PHP code in the page, not just static content?
'post_content' => 'Place all your body content for the post in this line.',
'post_status' => 'publish', // Automatically publish the post.
'post_type' => 'page', // defaults to "post".
'post_name' => 'my-plugin', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugin-page?/
);
// Lets insert the page now.
wp_insert_post( $post_data );
}
function plugin_activation() {
create__page();
}
function plugin_deactivation() {
}
register_activation_hook( __FILE__, 'plugin_activation' );
register_deactivation_hook( __FILE__, 'plugin_deactivation' );
You want wp_insert_post()
Usage:
$postarr = [
'post_title' => 'My Page Title', // <title> tag
'post_content' => '<p>page content</p>', // html for page content
'post_type' => 'page', // blog post is default
'post_name' => 'my-plugion-page', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugion-page?/
];
$post_id = wp_insert_post($postarr);
For example, in your $postarr if you want it to be a page (not a blog post), use 'post_type' => 'page'. For the page title 'post_title' => 'My New Page' and so on.
See: https://developer.wordpress.org/reference/functions/wp_insert_post/
Updated for additions to user question
If you don't want it to follow the theme templates, then you'll need to hook into the 'page_template' filter and use a template file you create in your plugin. See: https://wordpress.stackexchange.com/questions/3396/create-custom-page-templates-with-plugins
Depends on the situation, you can add code in your custom template, use shortcodes or custom Gutenberg blocks. You can use Advanced Custom Fields on in the Admin UI and use get_field() in your code, etc. You have lots of options in WordPress. Once you have a custom template you can just add regular PHP in there.
Yes, but you'll need to use regex and create rewrite rules using add_rewrite_rule(). Here are links that help with creating rewrite rules in WordPress
add_rewrite_rule
add_rewrite_tag
flush_rewrite_rules
WP_Rewrite API
I followed this tutorial to create custom endpoints. But, it always returns 404.
I added code below to function.php
add_action('rest_api_init', function() {
register_rest_route('awesome/v1', '/awesomeparams', array(
'methods' => 'GET',
'callback' => 'get_awesome_params',
'args' => array(),
'permission_callback' => function () {
return true;
}
));
});
function get_awesome_params( $data ) {
return "aaaa";
}
When I trying to access:
http://smap.cas.mcmaster.ca/wp-json/awesome/v1/awesomeparams
It shows 404:
I checked many other posts, but still can't find a solution. Can I get some help?
Thanks!
Did you flush your permalinks?
Go to Wordpress admin -> Settings -> Permalinks and click save, It should flush your permalinks and then It should work.
I am stuck on creating custom endpoints. I followed this tutorial to create custom endpoints. But, it always returns 404.
https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
I added code below to function.php:
add_action('rest_api_init', function() {
register_rest_route('awesome/v1', '/awesomeparams', array(
'methods' => 'GET',
'callback' => 'get_awesome_params',
'args' => array(),
'permission_callback' => function () {
return true;
}
));
});
function get_awesome_params( $data ) {
return "aaaa";
}
When I trying to access: http://www.my-domain.com/wp-json/awesome/v1/awesomeparams
It shows 404:
I also tried to flush the permalinks by going to WordPress admin -> Settings -> Permalinks and click save.
Can I get some help?
Thanks a lot!
The solution is to use the url below:
http://www.my-domain.com/index.php/wp-json/awesome/v1/awesomeparams