Skip to content

Instantly share code, notes, and snippets.

View Asikur22's full-sized avatar
๐Ÿ’œ
Web Developer | In Love with WordPress

Asiqur Rahman Asikur22

๐Ÿ’œ
Web Developer | In Love with WordPress
View GitHub Profile
@Asikur22
Asikur22 / functions.php
Created May 19, 2019 18:25
Add The CodeMirror Code Editor
// The HTML
echo '<textarea id="fancy-textarea">' . esc_textarea($val) . '</textarea>';
//Enqueing JS & CSS and setting up
function codemirror_enqueue_scripts($hook) {
$cm_settings['codeEditor'] = wp_enqueue_code_editor(array('type' => 'text/css'));
wp_localize_script('jquery', 'cm_settings', $cm_settings);
wp_enqueue_script('wp-theme-plugin-editor');
wp_enqueue_style('wp-codemirror');
@Asikur22
Asikur22 / functions.php
Last active May 19, 2019 18:30
Enqueue Scripts/Styles on the post-new.php and post.php pages in admin #SpecificPage
add_action( 'admin_print_scripts-post-new.php', 'your_prefix_enqueue_scripts' );
add_action( 'admin_print_scripts-post.php', 'your_prefix_enqueue_scripts' );
// OR
function enqueue_my_scripts() {
global $pagenow;
if ( ! empty( $pagenow ) && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) {
wp_enqueue_script();
}
@Asikur22
Asikur22 / functions.php
Created May 21, 2019 18:55
Get The First Image From a Post
/*
* Get The First Image From a Post
*/
function post_first_image() {
global $post;
ob_start();
ob_end_clean();
$output = preg_match_all( '/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches );
if ( $output != 0 ) {
@Asikur22
Asikur22 / snippets.js
Created June 6, 2019 09:53
Contact Form 7 Redirect based on Page URL
function cf7_redriect() {
var url = window.location.pathname.split('/');
if (typeof url[1] === 'undefined') {
window.location.href = 'https://www.liferesolutions.com.au/thank-you-contact-form/';
} else {
if (url[1] == 'eap') {
window.location.href = 'https://www.liferesolutions.com.au/thank-you-eap/';
} else {
window.location.href = 'https://www.liferesolutions.com.au/thank-you-contact-form/';
@Asikur22
Asikur22 / functions.php
Created June 14, 2019 18:46
Redirect to 404 Page
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 );
exit();
/**
* Show 404 No Found Page on Single View of Custom Post.
*/
function wpse_128636_redirect_post() {
@Asikur22
Asikur22 / functions.php
Created June 19, 2019 12:33
Show Specific Category Post on Custom Post Type Archive Page
/*
* Show Only One Category on Archive Page.
*/
function wp98_pre_get_posts( $query ) {
if ( is_post_type_archive( 'cpt_services' ) && $query->is_main_query() ) {
$query->set(
'tax_query', array(
array(
'taxonomy' => 'cpt_services_group',
'terms' => 'menu',
@Asikur22
Asikur22 / functions.php
Created June 19, 2019 12:34
Exclude categories on blog page
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-1,-1347' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
@Asikur22
Asikur22 / scripts.js
Created June 22, 2019 11:14
Stick Element #fixed
var stickyElement = $( "#list-faq-wrap" );
var stickyElementPos = stickyElement.offset();
$( window ).scroll( function () {
var windowpos = $( window ).scrollTop();
if ( windowpos >= stickyElementPos.top - 20 ) {
stickyElement.addClass( "stick" );
} else {
stickyElement.removeClass( "stick" );
}
} );
@Asikur22
Asikur22 / script.js
Last active December 24, 2023 19:43
WP Admin AJAX Call
const item = {
action: 'custom_add_to_cart',
product_id: Button.getAttribute( 'data-product_id' ),
quantity: document.querySelector( '.qty[name="quantity"]' ).value
};
var formData = new FormData();
for ( let key in item ) {
formData.append( key, item[key] );
}
@Asikur22
Asikur22 / functions.php
Created June 23, 2019 06:42
Remove Width and Height Attributes From Images
/**
* Remove Width and Height Attributes From Images
*/
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );