I'm trying to implement a custom JSON API upon Wordpress, like /api3/get_posts, I refer to the implementation of plugin JSON-API, but it just doesn't work. Below is my code:
$myPluginBase = 'api3';
function my_plugin_init() {
global $wp_rewrite;
add_filter('rewrite_rules_array', 'my_plugin_rewrites');
add_action('template_redirect', 'my_plugin_template_rewrite');
$wp_rewrite->flush_rules();
}
function my_plugin_template_rewrite(){
global $myPluginBase;
if( isset( $_REQUEST[ $myPluginBase ] ) ){
echo $_REQUEST[ $myPluginBase ];
exit;
}
}
function my_plugin_activation() {
// Add the rewrite rule on activation
global $wp_rewrite;
add_filter('rewrite_rules_array', 'my_plugin_rewrites');
$wp_rewrite->flush_rules();
}
function my_plugin_deactivation() {
// Remove the rewrite rule on deactivation
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function my_plugin_rewrites($wp_rules) {
global $myPluginBase;
if (empty($base)) {
return $wp_rules;
}
$my_plugin_rules = array(
"$myPluginBase\$" => "index.php?{$myPluginBase}=info",
"$myPluginBase/(.+)\$" => "index.php?{$myPluginBase}=\$matches[1]"
);
return array_merge($my_plugin_rules, $wp_rules);
}
// Add initialization and activation hooks
add_action('init', 'my_plugin_init');
register_activation_hook( __FILE__, 'my_plugin_activation');
register_deactivation_hook( __FILE__, 'my_plugin_deactivation');
It seems like the template_redirect action works, as I open /api3=hello, the server will send hello back. But if I try to open /api3/hello, it keeps showing me the home page.
Related
I'm quite new to WordPress Plugin development. I'm trying to use the plugin boilerplate to create a simple plugin for learning purpose.
I want to create a "Contact Form", the view will be located in /wp-content/plugins/contact-form/public/partials/contact-form-public-display.php. And I want to access it using http://localhost/contact-form/.
My class class-contact-form-public.php :
function init_internal_rewriting()
{
add_rewrite_rule( 'contact-form$', '?page=contact-form', 'top' );
}
function rewriting_query_vars( $query_vars )
{
$query_vars[] = 'page';
return $query_vars;
}
function rewriting_parse_request( &$wp )
{
if ( array_key_exists( 'page', $wp->query_vars ) ) {
$this->display_plugin_page();
exit();
}
return;
}
public function display_plugin_page() {
include_once( 'partials/contact-form-public-display.php' );
}
When I'm hooking those function :
// Rewriting URL
$this->loader->add_action( 'init', $plugin_public, 'init_internal_rewriting');
$this->loader->add_action( 'parse_request', $plugin_public, 'rewriting_parse_request');
$this->loader->add_filter( 'query_vars', $plugin_public, 'rewriting_query_vars' );
But I can't access the css and js.
Does someone have an idea to make it clearer ? Be able to access my "public" part in my frontend website.
Thank you for your help.
Create a short code for the contact form. And past the short code into page content section.
Check the link below how to create wordpress short code.
https://codex.wordpress.org/Function_Reference/add_shortcode
What im trying to accomplish is set different wordpress permalink for logged in users
For logged in users use: /loggedin/%post_id%/%postname%/ and for others use /post/%post_id%/%postname%/
Here is the PHP code which Im trying but not working
add_action( 'init', 'smartest_set_permalinks' );
function smartest_set_permalinks() {
global $wp_rewrite;
if(is_user_logged_in) {
$wp_rewrite->set_permalink_structure( '/loggedin/%post_id%/%postname%/' );
} else {
$wp_rewrite->set_permalink_structure( '/post/%post_id%/%postname%/' );
}};
What am i missing here can somebody point out or fix this?
You were missing the '()' after is_user_logged_in. is_user_logged_in() is a default wordpress function. And the semicolon in the end of function brace is not needed.
add_action( 'init', 'smartest_set_permalinks' );
function smartest_set_permalinks() {
global $wp_rewrite;
if(is_user_logged_in()) {
$wp_rewrite->set_permalink_structure( '/loggedin/%post_id%/%postname%/' );
} else {
$wp_rewrite->set_permalink_structure( '/post/%post_id%/%postname%/' );
}
}
I'm using Wordpress, where I have used my custom designed template, so I'm using custom generated URL's in most cases.
I want my URL to look like: website_url/details/191/
I want to rewrite this to more SEO friendly. I don't know how to write rewrite rules in htaccess and could use your help.
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );
// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['(details)/(\d*)$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
$newrules = array();
$newrules['(details)/(\d*)$'] = 'index.php? pagename=$matches[1]&id=$matches[2]';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
{
array_push($vars, 'id');
return $vars;
}
This is what I have added in my function.php but no change.
I'm trying to send a variable in the url. As I have the permalinks activated, I'm trying to add a rewrite rule, as said on a post I've read here...
Well nothing is working. This is the code I have on functions.php
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');
// Remember to flush_rules() when adding rules
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
$newrules = array();
$newrules['view-article/(.+)'] = 'index.php?pagename=view-article&aid=$matches[1]';
$finalrules = $newrules + $rules;
return $finalrules;
}
// Adding the var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
array_push($vars, 'aid');
return $vars;
}
//Stop wordpress from redirecting
remove_filter('template_redirect', 'redirect_canonical');
and this is the call to get the variable.
$aid = urldecode($wp_query->query_vars['aid']);
What am I doing wrong? :(
My wordpress version is 4.0 (The latest)
Try the following code:
add_action('init', 'flush_urls');
function flush_urls()
{
// Execute flush_rules() on every request can
// slow down your entire website
if('clean' !== get_option('flush_urls', ''))
{
flush_rewrite_rules();
add_option('flush_urls', 'clean');
}
}
add_action('init', 'register_rewrite_rules', 0);
function register_rewrite_rules()
{
add_rewrite_tag('%aid%', '([^/]+)');
add_rewrite_rule(
"view-article/([^/]+)/?",
'index.php?pagename=view-article&aid=$matches[1]',
"top"
);
}
Finally in your template-redirect or an any later action/hook you should do the following:
global $wp_query;
echo $wp_query->query_vars['pagename'];
echo $wp_query->query_vars['aid'];
In case you cannot see these variables inside the $wp_query, then try to save again the permalinks in your WordPress Dashboard. This will clean out the urls cache.
Example Code:
function flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function activate()
{
global $wp_rewrite;
createRewriteRules( $wp_rewrite );
flush_rewrite_rules();
}
function createRewriteRules( $rewrite )
{
global $wp_rewrite;
$new_rules = array( 'option/(.+)' => 'index.php?option=' . $wp_rewrite->preg_index(1) );
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules; // ERROR HERE............
return $wp_rewrite;
}
add_action( 'generate_rewrite_rules', createRewriteRules );
register_activation_hook( file, activate );
Sometimes gives Fatal error: Unsupported operand types, but activates the plugin and doesn't stop it from working... What am I doing wrong here?
Probably either $new_rules or $wp_rewrite->rules is not an array at the moment you want to combine them.
You could add a test to see if they are arrays, and if not, initialize them as an empty array.
(Addition:
Why pass $rewrite as an argument, when you use a global to get the original variable?)