Skip to content

Instantly share code, notes, and snippets.

View certainlyakey's full-sized avatar

Aleksandr Beliaev certainlyakey

  • Nortal AS
  • Tallinn
View GitHub Profile
@certainlyakey
certainlyakey / functions.php
Created March 13, 2018 22:36
SearchWP + Polylang — force search results to include untranslated post types
function searchwp_polylang_include_only_current_language_posts( $relevantPostIds, $engine, $terms ) {
if ( isset( $engine ) ) {
$engine = null;
}
if ( isset( $terms ) ) {
$terms = null;
}
@certainlyakey
certainlyakey / polylang.php
Last active July 2, 2025 11:27 — forked from maketheboat/pll_copy_post_metas.php
Wordpress and Polylang: prevent certain custom fields' values from synchronising between posts in different languages (they always sync by default)
<?php
// We could use wpml-config.xml to turn off the sync, but it wouldn't support repeater-like fields
function themeprefix_dont_sync_specific_fields( $metas ) {
$unsync = array(
'office_country',
'uspage_liftups_repeater'
);
// Support for repeaters, flexible content fields etc.
if ( is_array( $metas ) && is_array( $unsync ) ) {
@certainlyakey
certainlyakey / acf.php
Created February 19, 2018 23:17
Wordpress/ACF — remove current post from relationship fields which refer to the same post type
<?php
// Remove current post from relationship fields which refer to the same post type
function remove_current_post_from_self_relationships($args, $field, $post_id) {
$args['post__not_in'] = array($post_id);
return $args;
}
$self_relationship_fields = array('relation_cases_cases');
foreach ($self_relationship_fields as $key => $field_name) {
add_filter('acf/fields/relationship/query/name=' . $field_name, 'remove_current_post_from_self_relationships', 10, 3);
@certainlyakey
certainlyakey / admin-hooks.php
Created February 3, 2018 20:39
Hide YIKES Inc. Simple Taxonomy Ordering plugin settings page in the admin
<?php
// Hides YIKES Inc. Simple Taxonomy Ordering settings page in the admin
// we don't need it because the ordering is enabled from code (tax_position => true arg for register_taxonomy)
function hide_yikes_admin_page($capability_name) {
return 'create_sites';
}
add_filter('yikes_simple_taxonomy_ordering_capabilities', 'hide_yikes_admin_page');
@certainlyakey
certainlyakey / frontend-functions.php
Created February 1, 2018 15:07
Polylang — display flag or language code for term or post
<?php
function polylang_display_language_code_for_object($type = 'post', $id = null) {
$output = '';
if ( function_exists('pll_get_post_language') && function_exists('pll_get_term_language') ) {
// language slug
$slug = '';
@certainlyakey
certainlyakey / admin-hooks.php
Last active January 31, 2018 12:51
Add blog and front pages as shortcuts to Posts and Pages admin menus
<?php
function my_add_blog_archive_admin_menu() {
// $page_title, $menu_title, $capability, $menu_slug, $callback_function
add_posts_page(
__( 'Edit Archive Page', 'mysite' ),
__( 'Edit Archive Page', 'mysite' ),
'read',
add_query_arg(
array(
@certainlyakey
certainlyakey / admin-hooks.php
Created January 31, 2018 12:48
Prevent home and front pages deletion in admin
<?php
function prevent_pages_deletion( $allcaps, $caps, $args ) {
$pages_ids = array(
get_option( 'page_for_posts' ),
get_option( 'page_on_front' ),
);
// if using Polylang — don't forget to include specific language slug when using pll_get_post() to get the page id. Without it the function doesn't work in admin
// pll_get_post( get_option( 'page_for_posts' ), 'ru' ),
@certainlyakey
certainlyakey / fields-registration.php
Created December 5, 2017 09:02
ACF custom field repeater for tracking codes (Wordpress)
<?php
// can be in functions.php as well
acf_add_local_field_group(array (
'key' => 'group_59b665d4365e1',
'title' => 'Tracking codes',
'fields' => array (
array (
'key' => 'field_59b666088db92',
'label' => __('Tracking code (header)','theme_domain'),
@certainlyakey
certainlyakey / teasers.twig
Last active November 19, 2017 20:38
Easy way to alternate something (classes for example) every X and X times in Twig
{# Example: 2/6/2/6/2/6... #}
{# first batch + second batch sum #}
{% for batch in posts|batch(8) %}
{% for item in batch %}
{% set size = 'class-A' %}
{# first batch + 1 #}
{% if loop.index < 3 %}
@certainlyakey
certainlyakey / functions.php
Created November 3, 2017 15:47
Map Gravity forms file upload field to ACF user avatar image field (Wordpress)
function themeprefix_save_gf_upload_to_acf_image($entry, $form) {
// If the ID of the form being saved isn't the one we're looking for, bail.
$forms_with_class = themeprefix_get_form_ids_by_form_class('edit-profile');
if ( $forms_with_class[0] != $entry['form_id']) {
return;
}
// If we don't have the ID of the user, bail.
if (!isset($entry['created_by'])) {