Htaccess przekierowanie https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Podstawowe wp_query
$categories = new WP_Query(array(
'post_type' => 'page',
'posts_per_page' => 5,
'meta_key' => '_wp_page_template',
'meta_value' => 'templates/template-category.php',
));
while( $categories->have_posts() ) : $categories->the_post();
endwhile;
Pobieranie aktualnego obiektu (wynikającego z zapytania głównego, np kategorii w przypadku strony kategorii
$thisCat = $wp_query->get_queried_object();
Pobieranie zmiennej z url-a
if( isset( $_GET['multimedia'] ) ) {
$multimedia = $_GET['multimedia'];
};
Szablony dla podstron i dla konkretnych typów postów
* Template name: Strona główna * Template Post Type: landingPage
Wyciąganie id filmu z wszystkich linków YT
function youtube_id_from_url($url) {
$pattern =
'%^# Match any youtube URL
(?:https?://)? # Optional scheme. Either http or https
(?:www\.)? # Optional www subdomain
(?: # Group host alternatives
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com
(?: # Group path alternatives
/embed/ # Either /embed/
| /v/ # or /v/
| /watch\?v= # or /watch\?v=
) # End path alternatives.
) # End host alternatives.
([\w-]{10,12}) # Allow 10-12 for 11 char youtube id.
$%x'
;
$result = preg_match($pattern, $url, $matches);
if ($result) {
return $matches[1];
}
return false;
}
Ukrywanie paska admin-bar
add_action('after_setup_theme', 'remove_admin_bar'); function remove_admin_bar() { show_admin_bar(false); }
Pobranie miniatury konkretnego rozmiaru z id załącznika
$boxImageId = get_post_meta( get_the_id(), 'what_we_do_sub_image_id', true );
$boxImageId = get_post_thumbnail_id(); $boxImage = wp_get_attachment_image_src( $boxImageId, 'thumbnail' );
Dodanie nowego rozmiaru miniatur
add_image_size('poziomy-kolumna', 702, 401, true);
Nowy rozmiar miniatury w dropdownie
function add_image_size_to_media($sizes){
$custom_sizes = array(
'gallery-thumb’ => 'Kwadratowa miniatura’
);
return array_merge( $sizes, $custom_sizes );
}
add_filter(’image_size_names_choose’, 'add_image_size_to_media’);
Wyświetlenie listy wszystkich rozmiarów miniatur
get_intermediate_image_sizes()
ID strony ustawionej, jako strona główna postów
$page_for_posts = get_option( 'page_for_posts' );
ID strony ustawionej, jako główna
$frontpage_id = get_option( 'page_on_front' );
Paginacja
<div class="pager"><?php echo paginate_links( ); ?></div>
Przenoszenie strony, aktualizacja url-i, serialized data
Velvet blues przestał działać poprawnie, teraz trzeba spróbować tego go-live-update-urls
Przesyłanie zmiennych do zewnętrznej templatki:
Problem polega na tym że get_template_part() tego nie złapie, trzeba użyć includa: (działa również na child themes)
include(locate_template('template-parts/articles-list.php'));
Problem z cache szablonów podczas tworzenia
https://core.trac.wordpress.org/attachment/ticket/42573/42573.patch
Pobieranie parametru z URL
$searchWord = $_GET['s'];
Zmiana adresów url po wdrożeniu
Go Live Update URLS - CHUJA, nie działa już Można spróbować z search & replaceSearch & Replacealbo podobnych np better search replace https://interconnectit.com/products/search-and-replace-for-wordpress-databases/
TinyMCE edytor własne bloki, własne pola w edytorze
function add_style_select_buttons( $buttons ) {
array_unshift( $buttons, 'styleselect' );
return $buttons;
}
// Register our callback to the appropriate filter
add_filter( 'mce_buttons_2', 'add_style_select_buttons' );
//add custom styles to the WordPress editor
function my_custom_styles( $init_array ) {
$style_formats = array(
// These are the custom styles
array(
'title' => '3 columns',
'block' => 'p',
'classes' => 'three-col',
'wrapper' => true,
),
);
// Insert the array, JSON ENCODED, into 'style_formats'
$init_array['style_formats'] = json_encode( $style_formats );
return $init_array;
}
// Attach callback to 'tiny_mce_before_init'
add_filter( 'tiny_mce_before_init', 'my_custom_styles' );
Wtyczka do organizowania mediów
WP Media folders
Przekazanie zmiennej php do javascriptu
$dataToBePassed = array(
'a_value' => '10'
);
wp_enqueue_script( 'igsmie-calendar', get_template_directory_uri() . '/js/scripts-calendar.js', array(), '20151215', true ); wp_localize_script( 'igsmie-calendar', 'php_vars', $dataToBePassed );
następnie w js dobierasz się do tego poprzez php_vars.a_value
Wyrzucenie 404, wyświetlenie 404, zwrócenie 404
Aby zwrócić 404 za pomocą fukcji:
global $wp_query;
$wp_query->set_404();
status_header(404);
return get_404_template();
Nawigacja postów, post navigation
<div class="postNavigation">
<?php
$prev_post = get_previous_post();
$prev_post = $prev_post != '' ? get_permalink( $prev_post->ID ) : null;
$next_post = get_next_post();
$next_post = $next_post != '' ? get_permalink( $next_post->ID ) : null;
?>
<?php if( $prev_post ) { ?>
<a class="naviElem" href="<?php echo $prev_post; ?>">
<span class="naviArrow prev"></span>
<span class="naviText"><?php _e('previous', 'creativelabs'); ?></span>
</a>
<?php } else { ?>
<span class="naviElem disabled">
<span class="naviArrow prev"></span>
<span class="naviText"><?php _e('previous', 'creativelabs'); ?></span>
</span>
<?php } ?>
<?php if( $next_post ) { ?>
<a class="naviElem" href="<?php echo $next_post; ?>">
<span class="naviText"><?php _e('next', 'creativelabs'); ?></span>
<span class="naviArrow next"></span>
</a>
<?php } else { ?>
<span class="naviElem disabled">
<span class="naviText"><?php _e('next', 'creativelabs'); ?></span>
<span class="naviArrow next"></span>
</span>
<?php } ?>
</div>
Konwertowanie daty, data, format daty, tłumaczenie daty
$date = get_post_meta( get_the_ID(), 'meetings-date', true );
echo date_i18n( get_option( 'date_format' ), strtotime( $date ) );
Dodanie pola do rejestracji, rejestracja pole, rejestracja nowe pole
// Add the checkbox to registration form
add_action( 'register_form', 'foo_add_privacy_policy_field' );
function foo_add_privacy_policy_field() {
?>
<p>
<input type="checkbox" name="foo_privacy_policy" id="foo_privacy_policy" class="checkbox" style="float: left; height: 50px;" />
<label for="foo_privacy_policy">Zgadzam się na <a href="#">warunki</a>
</label>
</p>
<?php
}
// Validate the checkbox value in the registration form so that it is required
add_filter( 'registration_errors', 'foo_privacy_policy_auth', 10, 3 );
function foo_privacy_policy_auth( $errors, $sanitized_user_login, $user_email ) {
if ( !isset( $_POST['foo_privacy_policy'] ) ) :
$errors->add( 'policy_error', "<strong>ERROR</strong>: Please accept the privacy policy." );
return $errors;
endif;
return $errors;
}
// Fill the meta 'foo_privacy_policy' with the value of the checkbox
add_action( 'personal_options_update', 'foo_privacy_policy_save' );
add_action( 'edit_user_profile_update', 'foo_privacy_policy_save' );
add_action( 'user_register', 'foo_privacy_policy_save' );
function foo_privacy_policy_save( $user_id ) {
if ( isset( $_POST['foo_privacy_policy'] ) ){
update_user_meta( $user_id, 'foo_privacy_policy', $_POST['foo_privacy_policy'] );
}else{
update_user_meta( $user_id, 'foo_privacy_policy', 'off' );
}
}
// Add the checkbox to user profile home
add_action( 'show_user_profile', 'foo_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'foo_show_extra_profile_fields' );
function foo_show_extra_profile_fields( $user ) {
?>
<h3><?php esc_html_e( 'Privacy Policy', 'foo' ); ?></h3>
<table class="form-table">
<tr>
<td>
<input type="checkbox" name="foo_privacy_policy" id="foo_privacy_policy" class="checkbox" style="float: left; height: 50px;" <?php if(get_the_author_meta('foo_privacy_policy', $user->ID)=='on' ){ echo "checked"; } ?> />
<label for="foo_privacy_policy"><?php _e( 'Privacy Policy', 'foo' ) ?>
</label></td>
</tr>
</table>
<?php
}