CodeHQ

Posts Tagged ‘PHP’

« Older Entries |

Creating a custom route / endpoint in WordPress

Sunday, December 20th, 2020

The PHP

add_action(
	'rest_api_init',
	function () {
  		register_rest_route(
  			'myname/v1',
  			'/thethingimgetting',
  			array(
				'methods' => 'GET',
				'callback' => 'thefunctionwiththeloop',
			)
		);
	}
);
function thefunctionwiththeloop() {
	$args = array(
		'post_type' 	=> 'posts',
		'numberposts'   => -1,
	);
	$query = new WP_Query($args);
	$myPosts = array();
	if ($query->have_posts()) {
		foreach ($query->posts as $post) {
			$myPosts[] = $post;
		}
	}
	return rest_ensure_response($myPosts);
}

The JS

var api_url = '/wp-json/myname/v1/thethingimgetting';
$.ajax({
	url: api_url,
	method: 'GET'
})
.done(function(response){
	console.log(response);
})

Tags: , , , , ,
Posted in WordPress | No Comments »

Custom query on taxonomy page | WordPress

Thursday, June 14th, 2018

This will go in taxnonomy.php or taxonomy-name.php templates.

$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') ); 

$customField = get_field('custom_field_name')];
$args = array(
	'posts_per_page' => '-1',
	'post_type' 	 => 'service_centres',
	'tax_query' => array (
		array (
			'taxonomy' => 'regions',
			'field' => 'slug',
			'terms' =>  $term->slug
		)
	),
	'meta_key'	 => 'custom_field_name',
	'meta_value'	 => 'custom_field_value_value'
);

Tags: , , , , ,
Posted in PHP, WordPress | No Comments »

Grab first image from post

Monday, April 16th, 2018

Function for grabbing image from post and using it instead of a featured image for example.

Add this to functions.php

// Get URL of first image in a post
function catch_that_image() {
	global $post, $posts;
		$first_img = '';
		ob_start();
		ob_end_clean();
		$matches[1][0] = '';
		if(preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches)){
		  	$first_img = $matches[1][0];
		}

	// no image found display default image instead
	return $first_img;
}

And call in the theme with

<img src="<?php echo catch_that_image() ?>" onerror="this.style.display = 'none';"/> 

Tags: , , , , , ,
Posted in PHP, WordPress | No Comments »

Remove script and style version numbers | WordPress

Wednesday, November 30th, 2016

Add this to functions.php

// Pick out the version number from scripts and styles
function remove_version_from_style_js( $src ) {
    if ( strpos( $src, 'ver=' . get_bloginfo( 'version' ) ) )
        $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'remove_version_from_style_js');
add_filter( 'script_loader_src', 'remove_version_from_style_js');

Tags: , , , , , , ,
Posted in PHP, WordPress | No Comments »

Remove emoji code in header | WordPress

Thursday, October 6th, 2016

Add to functions.php

// REMOVE WP EMOJI
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');

remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );

Tags: , , , ,
Posted in PHP, WordPress | No Comments »

Add thumbnail theme support | WordPress

Friday, September 30th, 2016

Add this to the functions.php file.

add_theme_support( 'post-thumbnails' ); 

Tags: , , , ,
Posted in PHP, WordPress | No Comments »

Custom search form for WordPress

Friday, September 30th, 2016

Create a file named searchform.php and place this in the root of the theme folder. This will replace the default WordPress search form.

<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
    <input type="text" placeholder="<?php echo esc_attr_x( 'Search website...', 'placeholder' ) ?>"
            value="<?php echo get_search_query() ?>" name="s"
            title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>"/>
</form>

Tags: , , , , ,
Posted in PHP, WordPress | No Comments »

Exclude current post from WordPress query

Wednesday, August 17th, 2016

Get the current page ID and use that in the WordPress query to exclude that post from displaying.

<?php $currentID = get_the_ID();?>

<?php $args = array(
	'post_type' => 'post',
	'post__not_in' => array($currentID)
);

$query = new WP_Query($args);

 $i = 1;

while ( $query->have_posts() ) : $query-> the_post(); ?>

Tags: , , , ,
Posted in PHP, WordPress | No Comments »

Registration white list | WordPress

Friday, July 1st, 2016

Add this code in to your functions.php to create a domain whitelist for user registrations.

add_action('registration_errors', 'sizeable_restrict_domains', 10, 3);
function sizeable_restrict_domains( $errors, $login, $email ) {
	$whitelist = array(
        'domain.com', 
        'website.com'
    );
	if ( is_email($email) ) {
		$parts = explode('@', $email);
		$domain = $parts[count($parts)-1];
		$to = 'your@adminemail.com';
		$subject = 'Subject of email';
		$message = 'A user tried to register with the following email address: ' . $email;
		if ( !in_array(strtolower($domain), $whitelist) ) {
			$errors->add('email_domain', __('ERROR: You may only register with an approved email address.'));
		wp_mail( $to, $subject, $message );
		}
	}
	return $errors;
}

Tags: , , , , , , ,
Posted in PHP, WordPress | No Comments »

If user is logged in | WordPress

Tuesday, June 28th, 2016

Use to show content if a user is logged in.

<?php if ( is_user_logged_in() ) { ?>
  .....Content here......
<?php }  ?>

Use ! is_user_logged_in() if you want to show if NOT logged in.

Tags: , , , ,
Posted in PHP, WordPress | No Comments »

Next Page »