Skip to content

Instantly share code, notes, and snippets.

@arvindsvt
Forked from Sero88/wp-rewrite-plugin.php
Last active March 28, 2022 13:32
Show Gist options
  • Save arvindsvt/0287f6e9f81476ef71f19943997460b3 to your computer and use it in GitHub Desktop.
Save arvindsvt/0287f6e9f81476ef71f19943997460b3 to your computer and use it in GitHub Desktop.
https://www.youtube.com/watch?v=Q5QkgK3xcDo
<?php
/**
* @package WPCamp 2019
* @version 1.0
*/
/*
Plugin Name: rewrite api custom permalink - BUILD
Description: Creates custom post type and custom permalink
Author: Sergio
Version: 1.0
Author URI: esergio.com
DO NOT use this plugin as is for production. This plugin is for learning purposes only.
*/
class My_Plugin{
private $post_type = 'stores';
function __construct(){
//post type
//add_action('init', [$this, 'register_custom_post_type']);
//metabox
//add_action( 'load-post.php', [$this, 'state_metabox_setup'] );
//add_action( 'load-post-new.php', [$this, 'state_metabox_setup'] );
//add_filter( 'post_type_link', [$this,'replace_rewrite_tags'] , 10, 2 );
//query var
//add_filter ( 'query_vars', [$this, 'create_query_vars']);
//permalinks + rewrites
//add_action('init', [$this, 'my_rewrite_rule']);
//link query var
//add_action('pre_get_posts', [$this, 'before_posts']);
}
function register_custom_post_type(){
$args = [
'label' => 'Stores',
'public' => true,
'rewrite' => [
// permalink structure: store/store_state/store_name
'slug' => 'store/%store_state%',
'with_front' => false,
],
];
register_post_type(
$this->post_type,
$args
);
//DON'T RUN HERE ON PRODUCTION!!
flush_rewrite_rules();
}
function create_query_vars($vars){
$vars[] = 'store_state';
return $vars;
}
function replace_rewrite_tags($url, $post){
if ( is_wp_error($post) || $this->post_type != $post->post_type || empty($post->post_name) )
return $url;
//get store state from post_meta
$ba = strtolower(get_post_meta($post->ID, 'store_state', true));
return str_replace('%store_state%', $ba, $url);
}
/***
* Adds rewrite rule for locations post
**/
function my_rewrite_rule(){
add_rewrite_tag('%store_state%', '([^&]+)');
//interpret custom structure
//permalink structure: store/store_state/store-name
//add_rewrite_rule('^store/([^/]+)/([^/]+)/?','index.php?stores=$matches[2]&store_state=$matches[1]', 'top');
}
function before_posts($query){
//return if it is not the main query of it is the admin
if ( is_admin() || ! $query->is_main_query() ){
return;
}
$meta_query = [];
// add meta_query elements
$store_state = get_query_var('store_state');
if( !empty($store_state) ){
$meta_query[] = array( 'key' => 'store_state', 'value' => $store_state , 'compare' => '=' );
//if there are more meta_query then relation is AND
if( count( $meta_query ) > 1 ){
$meta_query['relation'] = 'AND';
}
//set meta_query to $query
if( count( $meta_query ) > 0 ){
$query->set( 'meta_query', $meta_query );
}
}
}
function state_metabox_setup(){
add_action( 'add_meta_boxes', [$this,'add_state_metabox']);
add_action( 'save_post', [$this, 'save_post_state'], 10, 2);
}
function build_state_meta(){
wp_nonce_field( basename( __FILE__ ), 'store_state_nonce' );
global $post;
$state = get_post_meta($post->ID, 'store_state', true);
$available_states = [
'Nebraska',
'Iowa',
'Colorado',
'Kansas'
]
?>
<select name="store_state">
<?php
foreach($available_states as $a_state){
$selected = $a_state == $state ? 'selected' : '';
?>
<option value="<?=$a_state?>"<?=$selected?>><?=$a_state?></option>
<?php
}
?>
</select>
<?php
}
function add_state_metabox(){
add_meta_box(
'store_state',
'State',
[$this,'build_state_meta'],
array('post', $this->post_type),
'side'
);
}
function save_post_state($post_id, $post){
if ( !isset( $_POST['store_state_nonce'] ) || !wp_verify_nonce( $_POST['store_state_nonce'], basename( __FILE__ ) ) ){
return $post_id;
}
$post_type = get_post_type_object( $post->post_type );
//can user edit post and is the post type store
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ){
return $post_id;
}
update_post_meta($post_id, 'store_state',sanitize_text_field($_POST['store_state']));
}
}
new My_Plugin();
function debug_func($query){
global $wp_rewrite;
echo "<pre>";
//print_r($query);
//print_r($wp_rewrite);
echo "</pre>";
//exit;
}
function query_debug(){
global $wp_query;
echo $wp_query->request;
}
if(!is_admin()){
//add_action('parse_request', 'debug_func');
//add_action('the_post', 'query_debug');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment