Skip to content

Instantly share code, notes, and snippets.

@getneerajk
getneerajk / get_terms_with_same_slug.php
Created December 30, 2024 05:00
2 terms with same slug #term #wp
<?php
/**
* Get terms with the same slug in two different taxonomies.
*
* @param string $taxonomy_1 The first taxonomy.
* @param string $taxonomy_2 The second taxonomy.
* @return array An associative array where the key is the term ID of taxonomy_1 and the value is the term ID of taxonomy_2.
*/
function get_terms_with_same_slug($taxonomy_1, $taxonomy_2) {
$terms_taxonomy_1 = get_terms([
@getneerajk
getneerajk / case_sensitive_username.php
Created March 13, 2024 07:14
Case sensitive username #hook #filer
<?php
function login_case_sensitive( $user, $username, $password ) {
$user_id = username_exists($username);
if( !$user_id ) {
return $user;
}
$user_obj = get_user_by('id', $user_id);
if( $username !== $user_obj->user_login ) {
return new WP_Error( 'authentication_failed', __( '<strong>ERROR</strong>: Invalid username or password.' ) );
@getneerajk
getneerajk / back2top.php
Created March 1, 2024 07:07
Back to top #back2top #wp #hook
<?php
//add the hook in page.php after entry-content
/* <?php do_action('si_after_entry_content_action'); ?>*/
// Back to Top
add_action( 'si_after_entry_content_action', function () { if ( !is_admin() ) {
?>
<div id="back-to-top" class="back-to-top" >
<!--add image here-->
@getneerajk
getneerajk / si_announcement.php
Created February 27, 2024 07:02
Header Announcement #announcement #hook
function si_announce_functions() {
if (!isset($_COOKIE['announcement_closed']) && !is_user_logged_in()) {
?>
<div id="si-ann-main-" class="si-announce-main-div">
<div class="si-announce-main si-container">
<p class="cookie-announce-para">
Become a local news contributor! <a class="si-announcement-link" href="/login/" target="_blank">Sign up for free</a> and start posting your events and news today.
</p>
<span class="close-btn-announce" onclick="hideAnnouncement()">
@getneerajk
getneerajk / animate_count.js
Created February 16, 2024 09:06
Animate numbers count #animation #count #js
document.addEventListener("DOMContentLoaded", function() {
/* animate counter
* <span class="animate-count">1000</span>
*/
function isElementInViewport(el) {
var rect = el.getBoundingClientRect();
@getneerajk
getneerajk / safari_ios_check.js
Created February 13, 2024 07:05
Check if current browser is Safari iOS
var ua = window.navigator.userAgent;
var iOS = ua.match(/iPad/i) || ua.match(/iPhone/i);
var webkit = ua.match(/WebKit/i);
var iOSSafari = iOS && webkit && !ua.match(/CriOS/i);
console.log("ios: "+ iOS + " | webkit: "+ webkit +" | ios safari: "+ iOSSafari);
@getneerajk
getneerajk / search_result_count.php
Created December 1, 2023 03:59
Search result count #wp #search
<?php
$query_cpt = new WP_Query($args);
$posts_per_page = $query_cpt->query_vars['posts_per_page'];
$posts_found = $query_cpt->found_posts;
$paged = $query_cpt->query_vars['paged'];
if ( $posts_found ) {
$start_result = ( $paged - 1 ) * $posts_per_page + 1;
@getneerajk
getneerajk / single_image_uploads_dir.php
Last active September 21, 2024 21:01
WP All Import Pro upload image to its original path #wp #WP_All_Import_Pro
<?php
add_filter( 'wp_all_import_single_image_uploads_dir', 'wpai_wp_all_import_single_image_uploads_dir', 10, 6 );
function wpai_wp_all_import_single_image_uploads_dir( $uploads, $image_url, $articleData, $current_xml_node, $import_id, $post_id ) {
$position = strpos($image_url, 'uploads');
$result = substr($image_url, $position + 7,8); //eg: `/2021/05`
$uploads['path'] = $uploads['basedir'].$result;
$uploads['url'] = $uploads['baseurl'].$result;
$uploads['subdir']=$result;
@getneerajk
getneerajk / csp.php
Created November 15, 2023 03:58
Set csp headers #wp
<?php
// Add a filter to modify the HTTP headers
function add_csp_header() {
header("Content-Security-Policy: default-src 'self'; script-src 'self' https://example.com; style-src 'self' https://example.com;");
// Add more directives as needed based on your specific requirements
}
// Hook the function to the 'send_headers' action
add_action('send_headers', 'add_csp_header');
@getneerajk
getneerajk / tax_sync.php
Created November 8, 2023 13:07
Synchronize terms between the two taxonomies #taxonomy #wp
<?php
// Synchronize terms between the two taxonomies
function synchronize_taxonomies( $term_id, $tt_id, $taxonomy ) {
if (in_array($taxonomy, array('first_taxonomy', 'second_taxonomy'))) {
$term = get_term($term_id, $taxonomy);
$other_taxonomy = ($taxonomy === 'first_taxonomy') ? 'second_taxonomy' : 'first_taxonomy';
// Check if the term exists in the other taxonomy
$term_exists = term_exists($term->name, $other_taxonomy);