Skip to content

Instantly share code, notes, and snippets.

@nikolov-tmw
nikolov-tmw / MySampleClass.php
Created April 23, 2024 18:03
Maybe-kinda-late-initializing classes in WordPress?
<?php
class MySampleClass {
use Hookable;
public static function register_hooks() {
static::add_action( 'wp_footer' );
static::add_filter( 'the_content', 100 );
static::add_filter( 'admin_title', 100, 2 );
import { Fragment } from "@wordpress/element";
import { createHigherOrderComponent } from "@wordpress/compose";
import { addFilter } from "@wordpress/hooks";
import { TextControl } from "@wordpress/components";
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { useBlockProps, PlainText } from '@wordpress/block-editor';
const AddSubtitle = createHigherOrderComponent((BlockEdit) => {
const AddSubtitle = (props) => {
@nikolov-tmw
nikolov-tmw / custom-menu-panel.php
Last active February 22, 2024 21:29
This registers a custom meta box for nav menus and renders it. Obviously $my_items would ideally be not hard-coded and instead it would come somewhere from the DB. The custom items add to the menu and save properly, but will probably not be displayed correctly. You might need to hook to the 'wp_setup_nav_menu_item' filter in order to fix the men…
<?php
function my_register_menu_metabox() {
$custom_param = array( 0 => 'This param will be passed to my_render_menu_metabox' );
add_meta_box( 'my-menu-test-metabox', 'Test Menu Metabox', 'my_render_menu_metabox', 'nav-menus', 'side', 'default', $custom_param );
}
add_action( 'admin_head-nav-menus.php', 'my_register_menu_metabox' );
/**
@nikolov-tmw
nikolov-tmw / multiple-roles-per-user.php
Last active October 5, 2021 03:56
Multiple roles per user WordPress plugin.
<?php
/**
* Plugin Name: Multiple Roles per User
* Description: Allows anyone who can edit users to set multiple roles per user. In a default WordPress environment that wouldn't have much of an effect, since the user would have the privileges of the top-privileged role that you assign to him. But if you have custom roles with custom privileges, this might be helpful.
* Version: 1
* Author: nikolov.tmw
* Author URI: http://paiyakdev.com/
* License: GPL2
*/
/*
@nikolov-tmw
nikolov-tmw / functions.php
Created September 4, 2014 22:54
Reusable WP template for PHP and JS
<?php
function load_js_template(){
get_template_part( 'sample-template' );
}
add_action( 'wp_footer', 'load_js_template' );
function render_template( $template_name, $tmpl_data ) {
// "/" should not be present, but let's sanitize just in case
$template_name = str_replace( '/', '_', $template_name );
@nikolov-tmw
nikolov-tmw / .htaccess
Last active December 28, 2015 00:09
WordPress login lock - try to prevent automated brute force attacks. Just place the login-lock.php file in /wp-content/mu-plugins/ and add the code in .htaccess to your root's .htaccess file. Note that if you have WordPress in a sub-directory, you would have to change the RewriteBase on line 8.
# Add this code to the .htaccess in your root WordPress directory
# If your wordpress files are in a sub-directory, just change the RewriteBase
# to something like:
# RewriteBase /wordpress/
# If your wordpress files are in a sub-directory called "wordpress"
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Redirect to home page upon log-out
@nikolov-tmw
nikolov-tmw / functions.php
Created October 6, 2015 15:42
Automatic Administrator(or any user) log-in for WordPress. Just add the code anywhere in the active theme's functions.php and follow the instructions
<?php
function login_user_by_un_id( $un = false, $id = false ) {
$user = $un ? get_user_by( 'login', $un ) : get_user_by( 'id', intval( $id ) );
if ( $user && ! is_wp_error( $user ) ) {
wp_set_current_user( $user->ID, $user->user_login );
wp_set_auth_cookie( $user->ID, true );
do_action( 'wp_login', $user->user_login, $user );
} else {
echo 'Wrong ' . ( $un ? 'username' : 'user ID' ) . '!';
@nikolov-tmw
nikolov-tmw / class-post-by-email.php
Last active August 29, 2015 14:15
Inline image replacement
<?php
// This is right after
// if ( $attachment_count > 0 && ! has_shortcode( $post_content, 'gallery' ) ) {
// I realize now that it should actually be in a separate if that doesn't include the ! has_shortcode() condition
// 3D seems to be included when sending email via Apple Mail like so:
// src=3D"...."
preg_match_all( '/<img(.*?)src=3?D?[\'"]cid:([^"\']*)[\'"]([^<>]*)\/?>/ims', $post_content, $inline_image_matches );
if ( $inline_image_matches ) {
$search_array = array();
@nikolov-tmw
nikolov-tmw / disable-social.php
Created July 18, 2014 15:42
Disables displaying of the social sharing buttons by the "Tweet, Like, Google +1 and Share" plugin on specific page templates
<?php
add_action( 'template_redirect', 'maybe_disable_social_buttons' );
function maybe_disable_social_buttons() {
if ( is_page() && 'no-social-page.php' == get_page_template_slug( get_the_ID() ) ) {
remove_filter( 'the_content', 'disp_social',1 );
}
}