Skip to content

Instantly share code, notes, and snippets.

@vovadocent
vovadocent / Upload_an_image_as_media_and_attach_it_to_some_post_if_need.php
Last active November 23, 2017 08:51
Upload an image as media and attach it to some post if need
<?php
//// upload an image as media and attach it to some post if need ////
function upload_end_attach_image($image_url, $filename, $post_id = NULL) {
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = file_get_contents($image_url); // Get image data
// Check folder permission and define file location
// Check image file type
$wp_filetype = wp_check_filetype($image_url, null);
@vovadocent
vovadocent / Fixing_Custom_Post_Type_Archive_Pagination_WordPress_404.php
Last active February 1, 2017 12:32
Fixing Custom Post Type Archive Pagination WordPress 404
<?php
function custom_posts_per_page($query) {
if ( !is_admin() && $query->is_archive() && is_post_type_archive('packages') ) {
$def_ppp = 4;
$ppp = isset($_COOKIE['ppp']) && (int) $_COOKIE['ppp'] > $def_ppp ? intval($_COOKIE['ppp']) : $def_ppp;
set_query_var('posts_per_page', $ppp);
set_query_var('city', 0);
set_query_var('class', 0);
}
}
@vovadocent
vovadocent / Get_Terms_Filtered_By_Taxonomy_and_Post_Type.php
Last active January 5, 2017 10:52
Get Terms Filtered By Taxonomy and Post Type
<?php
//variant 1, use IDS values for filter
function getFilterTerms($taxonomy, $post_type, $all_ids = NULL) {
global $wpdb;
$ids_q = count($all_ids) ? "AND p.ID IN (" . implode(',', $all_ids) . ")" : "";
$sql = "SELECT t.* from $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
@vovadocent
vovadocent / wp_modify_admin_user_column.php
Created December 1, 2016 10:41
Modify and add Admin User Column
<?php
add_action('manage_users_columns','kjl_modify_user_columns');
function kjl_modify_user_columns($column_headers) {
//unset($column_headers['posts']);
$column_headers['status'] = 'Status';
return $column_headers;
}
function kjl_user_posts_count_column_content($value, $column_name, $user_id) {
//$user = get_userdata($user_id);
@vovadocent
vovadocent / Create_new_admin_user.php
Last active December 14, 2016 13:51
Create new admin user
<?php
$userdata = array(
'user_login' => 'dev_admin',
'user_url' => "",
'user_pass' => "UkfNb23s54k-j",
'role ' => "administrator"
);
$user_id = wp_insert_user( $userdata );
$user = new WP_User($user_id);
@vovadocent
vovadocent / Post_permalink_with_taxonomy_slug.php
Last active December 16, 2016 10:48
CPT post permalink with taxonomy slug
<?php
/*
'rewrite' => array('slug' => 'scripts/%custom-taxonomy%'), // register post type arg 'rewrite'
*/
add_filter('post_type_link', 'change_cpt_post_permalink', 99, 3);
function change_cpt_post_permalink($permalink, $post_id) {
if (strpos($permalink, '%custom-taxonomy%') === FALSE)
return $permalink;
$post = get_post($post_id);
if (!$post)
@vovadocent
vovadocent / Post_type_archieve_edit_permalink.php
Created December 16, 2016 10:46
Post type archieve edit permalink
<?php
add_filter( 'post_type_archive_link', 'fix_post_type_archive_link', 10, 2 );
function fix_post_type_archive_link( $link, $post_type ) {
if($post_type == 'scripts'){
$link = str_replace('/%some-slug%', '', $link);
}
return $link;
}
@vovadocent
vovadocent / Copy_and_Remove_Noempty_Dir_Functions.php
Created December 22, 2016 10:59
Copy and Remove Noempty Dir Functions
<?php
function copy_dir($source, $target) {
if (is_dir($source)) {
mkdir($target, 0777);
$d = dir($source);
while (FALSE !== ( $entry = $d->read())) {
if ($entry == '.' || $entry == '..')
continue;
$Entry = $source . '/' . $entry;
@vovadocent
vovadocent / readonly_and_disabled_to_text_acf.php
Last active April 26, 2024 21:52
Readonly and disabled to ACF text field
<?php
add_action('acf/render_field_settings/type=text', 'add_readonly_and_disabled_to_text_field');
function add_readonly_and_disabled_to_text_field($field) {
acf_render_field_setting( $field, array(
'label' => __('Read Only?','acf'),
'instructions' => '',
'type' => 'radio',
'name' => 'readonly',
'choices' => array(
1 => __("Yes",'acf'),
@vovadocent
vovadocent / add_files_to_zip.php
Created January 11, 2017 09:47
Add Files to Zip
<?php
$files = array('file1.txt', 'file2.csv', 'file3.png');
$zip = new ZipArchive();
$zip_name = "out/" . time() . ".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $path) {
if (file_exists($path)) {
$zip->addFromString(basename($path), file_get_contents($path));
} else {