Skip to content

Instantly share code, notes, and snippets.

View DevWael's full-sized avatar
👨‍💻
WordPress-ing

Ahmad Wael DevWael

👨‍💻
WordPress-ing
View GitHub Profile
@DevWael
DevWael / admin_menu_pages.php
Created June 27, 2019 11:33
create wordpress admin menu links and sub links dynamically with external redirects functionality
<?php
function idl_menu_build_data() {
$data = [
[
'slug' => 'rtdhrt',
'page_title' => 'first page',
'menu_title' => 'first page',
'cap' => 'manage_options',
'callable_function' => '',//write a function that holds all content of this page or leave it empty to listen to the redirect url
@DevWael
DevWael / woocommerce_products_manager.php
Last active June 24, 2019 12:49
Custom user roles for products and orders management in woocommerce (Products Manager & Orders Manager)
<?php
//put the following code in a custom plugin file
//no need for customizing the code, just go and create new users with the new user roles appeared in dashboard
register_activation_hook( __FILE__, 'wsd_plugin_activate' );
function wsd_plugin_activate() {
add_role( 'wsd_orders_manager', esc_html__( 'Orders Manager', 'wsd' ), [
'read' => true, // must have to even access /wp-admin
'edit_posts' => true, // without this the user is redirected to WooCommerce account page
'publish_posts' => false,
'read_shop_order' => true,
@DevWael
DevWael / trello_create_card.txt
Created June 18, 2019 09:40
create a card on trello prgramatically
//Get all lists from a board (GET Request)
//useful to get the list ID
https://trello.com/1/boards/{BOARD-ID}/lists?cards=all&card_fields=all&filter=open&fields=name&key=API-KEY&token=YOUR-TOKEN
//Create a card on a list (POST Request)
https://api.trello.com/1/cards?key=API-KEY&token=YOUR-TOKEN&name=CARD-NAME&desc=DESCRIPTION&idList=LIST-ID
@DevWael
DevWael / login_with_username.php
Created June 17, 2019 10:09
Log users with only (Username) In WordPress (Login without PASSWORD!!)
<?php
//create a function and use the following methods to log the user in without password in wordpress
$username = $_POST['username'];
$user = get_user_by('login', $username );
wp_clear_auth_cookie();
wp_set_current_user ( $user->ID );
wp_set_auth_cookie ( $user->ID );
@DevWael
DevWael / override_wc_templates_plugin.php
Created June 12, 2019 13:41
Override Woocommerce templates from a WordPress plugin
<?php
add_filter( 'woocommerce_locate_template', 'inno_oc_woocommerce_locate_template', 10, 3 );
function inno_oc_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) {
$template_path = $woocommerce->template_url;
}
$plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/woocommerce/';
// Look within passed path within the theme - this is priority
@DevWael
DevWael / create_user.php
Created May 12, 2019 09:07
Create WordPress admin user with php
<?php
//put the following code in functions.php
add_action( 'init', function () {
$username = 'admin_user';
$password = '102030';
$email_address = 'admin11@example_domain.com';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
@DevWael
DevWael / plugin_description_links.php
Created May 7, 2019 12:37
add links to plugin description
@DevWael
DevWael / disable_gutenberg.php
Last active January 16, 2020 12:03
Disable New WP Editor (Gutenberg)
<?php
//Put the following snippet in functions.php
//disable gutenberg editor
add_filter( 'use_block_editor_for_post', '__return_false', 10 );
add_filter( 'use_block_editor_for_page', '__return_false', 10 );
@DevWael
DevWael / ulike_user_liked_posts.php
Created March 11, 2019 10:20
get user liked posts (IDs) with plugin (WP Ulike)
<?php
function dw_get_posts_liked_by_user( $user_ID = '' ) {
if ( function_exists( 'wp_ulike' ) ) {
global $wpdb;
$result = array();
if ( ! $user_ID ) {
$user_ID = get_current_user_id();
}
$likes = $wpdb->get_results( "
SELECT U.post_id
@DevWael
DevWael / generate_form_inputs.php
Created February 16, 2019 16:35
generate form inputs (untested)
<?php
add_action( 'add_item_form_html', 'dw_add_item_form' );
function dw_add_item_form() {
$args = [
[
'type' => 'wrapper_start',
'class' => 'row',
],
[