Skip to content

Instantly share code, notes, and snippets.

@eto4detak
Last active September 10, 2019 18:13
Show Gist options
  • Save eto4detak/5ddcb45b9c20003a7aaf1809e06d7f67 to your computer and use it in GitHub Desktop.
Save eto4detak/5ddcb45b9c20003a7aaf1809e06d7f67 to your computer and use it in GitHub Desktop.
wp php
<?php
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );
define( 'WP_DEBUG_LOG', true );
// role
add_action('init', 'dsgsdhgsafasfasfasf');
function dsgsdhgsafasfasfasf($value='')
{
$role = get_role('autodealer');
$role->add_cap('read');
$role->add_cap('delete_posts');
$role->add_cap('edit_posts');
$role->add_cap('upload_files');
// $role->remove_cap('upload_files');
}
add_action('init', 'eto_admin_role_footer_322141');
function eto_admin_role_footer_322141($value='')
{
if( current_user_can('administrator') ){
$users = get_users( array(
'role' => 'Participant',
) );
$emails = wp_list_pluck( $users, 'user_email' );
echo '<pre class="aaa" style="display:none">';
var_dump($emails);
echo '</pre>';
}
}
// update option
if($_POST['fontColourHexOptions'] && $_POST['fontColourOptions'] ) {
$hex = "#".$_POST['fontColourHexOptions'];
$colourName = $_POST['fontColourOptions'];
if(!$stickertime_font_colour_options) {
$stickertime_font_colour_options = array(array("hex" => $hex,"colourName" => $colourName ));
} else {
array_push($stickertime_font_colour_options,array("hex" => $hex,"colourName" => $colourName));
}
//print_r($stickertime_font_colour_options);
sort($stickertime_font_colour_options);
update_option('stickertime_font_colour_options',$stickertime_font_colour_options);
}
// include
get_template_part('inc/parse');
// временные
set_transient( 'special_query_results', $special_query_results, 12 * HOUR_IN_SECONDS );
$special_query_results = get_transient( 'special_query_results' );
if ( false === $special_query_results ) {
$special_query_results = 'data';
set_transient( 'special_query_results', $special_query_results, 12 * HOUR_IN_SECONDS );
}
// nonce
check_ajax_referer( 'process_reservation_nonce', 'nonce' );
wp_nonce_field( 'seller_auto_upload', 'seller_auto_upload_nonce' );
// response
wp_send_json_success($html);
wp_send_json_error( array( 'error' => $custom_error ) );
// add to js
add_action( 'wp_enqueue_scripts', 'art_push_js_filter_data', 999 );
function art_push_js_filter_data(){
wp_localize_script( 'jquery-fancybox', 'rrrrrr', array(
'some_string' => __( 'Some string to translate' ),
'a_value' => '10'
) );
}
// rule admin user
if( current_user_can('manage_options') ){
echo "У пользователя есть права изменять настройки";
}
// delete codex_postmeta
add_action( 'admin_init', 'codex_init' );
function codex_init() {
add_action( 'delete_post', 'codex_sync', 10 );
}
function codex_sync( $pid ) {
global $wpdb;
if ( $wpdb->get_var( $wpdb->prepare( 'SELECT post_id FROM codex_postmeta WHERE post_id = %d', $pid ) ) ) {
$wpdb->query( $wpdb->prepare( 'DELETE FROM codex_postmeta WHERE post_id = %d', $pid ) );
}
}
// moving file
$old_name = get_attached_file(9974);
$new_name = wp_upload_dir()['basedir']."/cat/sagsaffdshfdsfaasfasf-18.jpg" ;
if(!file_exists($old_name) || file_exists($new_name))
{
echo "Not old name or " ;
}
else
{
if(rename( $old_name, $new_name))
{
echo "Successfully Renamed $old_name to $new_name" ;
}
else
{
echo "A File With The Same Name Already Exists" ;
}
}
//войти под админом
$user_id = 1;
$user = get_user_by( 'id', $user_id );
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login );
}
// image
$image_arr = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
echo get_the_post_thumbnail( $page->ID, 'thumbnail');
/////////////////////////template_redirect//////////////////////////
add_action( 'template_redirect','access_woocommerce_hide_non_registered' );
function access_woocommerce_hide_non_registered() {
global $woocommerce;
if( is_cart()) {
// $checkout_url = $woocommerce->cart->get_checkout_url();
wp_redirect( site_url( '/' ) );
exit();
}
}
add_action( 'template_redirect', 'sibl_redirect_new_url');
function sibl_redirect_new_url() {
$url = $GLOBALS['_SERVER']['REDIRECT_URL'];
// portfolio --> vse_proekty_domov_i_ban
if( $url === '/portfolio/' ){
wp_redirect( home_url( '/vse_proekty_domov_i_ban/' ), 301 );
die;
}
// /category/srub-bani-iz-brevna-proekty/view-bol/ --> /category/srub-bani-iz-brevna-proekty/
if( $url === '/category/srub-bani-iz-brevna-proekty/view-bol/' ){
wp_redirect( home_url( '/category/srub-bani-iz-brevna-proekty/' ), 301 );
die;
}
// from CAPS to low urls
$low_url = mb_strtolower($url);
if($low_url != $url){
wp_redirect( home_url( $low_url ), 301 );
die;
}
}
// вывести все хуки
function custom_function_hooks_list( $hook_name = '' ){
global $wp_filter;
$wp_hooks = $wp_filter;
// для версии 4.4 - переделаем в массив
if( is_object( reset($wp_hooks) ) ){
foreach( $wp_hooks as & $object ) $object = $object->callbacks;
unset($object);
}
if( $hook_name ){
$hooks[ $hook_name ] = $wp_hooks[ $hook_name ];
if( ! is_array($hooks[$hook_name]) ){
trigger_error( "Nothing found for '$hook_name' hook", E_USER_WARNING );
return;
}
}
else {
$hooks = $wp_hooks;
ksort( $wp_hooks );
}
$out = '';
foreach( $hooks as $name => $funcs_data ){
ksort( $funcs_data );
$out .= "\nхук\t<b>$name</b>\n";
foreach( $funcs_data as $priority => $functions ){
$out .= "$priority";
foreach( array_keys($functions) as $func_name ) $out .= "\t$func_name\n";
}
}
return $out ;
}
/*========================================================
* load core
========================================================*/
require_once '../../../wp-load.php';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment