Skip to content

Instantly share code, notes, and snippets.

@bagerathan
bagerathan / share-slug.php
Created November 9, 2023 04:00
[Share slug between sub page and post type]
add_action( 'parse_request', 'my_parse_request' );
function my_parse_request( $wp ) {
$post_type = 'whatever'; // set the post type slug
// and the "whatever" below is the Page slug
// This code checks if the path of the current request/page URL begins with
// "whatever/" as in https://example.com/whatever/child-page-name and also
// https://example.com/whatever/child-page-name/page/2 (paginated request).
// We also check if the post_type var is the post type set above.
if ( preg_match( '#^whatever/#', $wp->request ) &&
@bagerathan
bagerathan / cpt.html
Created May 17, 2023 05:08
[Ajax search] Search custom post type
<form role="search" method="post" class="search-form padding-4" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
<input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search...', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
<input class="post_type" type="hidden" name="post_type" value="frequent" />
</label>
<input type="submit" class="search-submit button brand" id="searchsubmit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
</form>
<script type="text/javascript">
@bagerathan
bagerathan / change-permalinks.php
Created June 16, 2022 06:03
[Change permalinks]
@bagerathan
bagerathan / protect-media-files.htaccess
Created November 29, 2021 03:13
[Protect WP media files] #htaccess
# Protect WP Media Files
# https://m0n.co/protect-media-files
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /wp-content/uploads/ [NC]
RewriteCond %{HTTP_REFERER} !^https://example.com [NC]
RewriteRule .* - [F,L]
</IfModule>
@bagerathan
bagerathan / add-search-to-wordpress-menu.php
Created August 27, 2021 07:19
[Add anything to menu] #wp
//add search to menu
function add_last_nav_item($items, $args) {
if ('menu' == $args->theme_location) {
$searchblock = get_search_form(false);
$items .= '<li><svg class="menu-search" xmlns="http://www.w3.org/2000/svg" width="21.472" height="20.171" viewBox="0 0 21.472 20.171"><path d="M29.053,20.3a8.766,8.766,0,0,0-8.525,6.033,8.089,8.089,0,0,0,3.877,9.351,9.252,9.252,0,0,0,10.613-1l6.144,5.782.5-.471-6.108-5.782a7.933,7.933,0,0,0,1.576-8.956A8.861,8.861,0,0,0,29.053,20.3Zm0,15.935A7.881,7.881,0,0,1,20.944,28.6a7.881,7.881,0,0,1,8.109-7.631A7.881,7.881,0,0,1,37.162,28.6,7.881,7.881,0,0,1,29.053,36.235Z" transform="translate(-20.191 -20.3)" fill="#fff"/></svg>'.$searchblock.'</li>';
return $items;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'add_last_nav_item', 10, 2 );
@bagerathan
bagerathan / add-content-before-and-after-menu.php
Created August 23, 2021 07:12
[My Account page snippets] #woo
add_action('woocommerce_before_account_navigation', 'misha_some_content_before');
function misha_some_content_before(){
echo 'blah blah blah before';
}
add_action('woocommerce_after_account_navigation', 'misha_some_content_after');
function misha_some_content_after(){
?>
<p>blah blah blah after</p>
<?php
@bagerathan
bagerathan / posts-from-same-month.php
Created August 23, 2021 06:16
[Posts from same month] #wp
$start_date = date( 'ym01' );
$end_date = date( 'ymt' );
$meta_query = array(
'key' => 'event_date',
'value' => array( $start_date, $end_date ),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
);
$args = array(
'post_type' => 'hh_event',
@bagerathan
bagerathan / wordpress-conditions.php
Last active June 16, 2022 06:04
[WordPress conditions] #wp
<?php
is_home() //Checks if the blog post index is being displayed. This may or may not be your home page as well.
is_front_page() //Checks if your home page is being displayed. This works whether your front page settings are set up to display blog posts (i.e. blog index) or a static page.
is_single() //Checks to see whether any type of single post is being displayed (excluding attachments).
is_attachment() //Checks if an attachment is displayed.
@bagerathan
bagerathan / compress-files.htaccess
Last active August 23, 2021 06:28
[htaccess] #web
<IfModule mod_deflate.c>
# Compress all output labeled with one of the following MIME-types
# (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
# and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines
# as `AddOutputFilterByType` is still in the core directives).
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
@bagerathan
bagerathan / restrict-access.php
Created August 23, 2021 04:25
[Restrict access to custom posts based on role] #wp
//create a new role
function psp_add_project_management_role() {
add_role('psp_project_manager',
'Project Manager',
array(
'read' => true,
'edit_posts' => false,
'delete_posts' => false,
'publish_posts' => false,