This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

Using cURL to Determine the Status Code of a URL

Whenever we’re working with remote requests within WordPress, we’re likely using one of the functions that are available through the core API. These are functions such as:

And they’re great and I recommend using them.

Depending on the project, you may need to determine the HTTP status of the page before making the request. And if you want to do that, I recommend two things:

  1. HTTP Status Codes
  2. The code below.

For example, the following code determines the status code of a URL using cURL:

<?php
/**
 * Determines if a specific URL returns a valid page. This is experimental and it is based on
 * the status code.
 *
 * @param string $url the url to evaluate
 *
 * @return bool true if the URL returns a status code of 404; otherwise, false
 */
public function isValidUrl(string $url): bool
{
	$curl = curl_init($url);
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	$response = curl_exec($curl);
  
	$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  	curl_close($curl);
  
  	return (404 !== $httpCode);
}

@source: https://gist.github.com/tommcfarlin/2ad96af9aa3007807686ac87b630dcd7#file-00-is-valid-url-php

Of course, you can replace 404 with any value from the above page to evaluate it to any given status you need.

Regardless, before making a request to a given URL, this particular function can help you determine the course of action you need to take in your project before actually making a request and have it fail.

Add a Custom Post Type Submenu To An Existing Custom Post Type Menu

Custom Post Types are on of the most powerful features of WordPress, especially if you’re in the business of creating custom solutions for clients beyond normal blogging functionality.

Introducing a new menu in the WordPress dashboard is really easy when using the custom post type API; however, what about the case when you have a custom post type and is a child of another custom post type?

Specifically, what about the case when you want to add a custom post type submenu to an existing custom post type menu?

Continue reading “Add a Custom Post Type Submenu To An Existing Custom Post Type Menu”

Programmatically Create a WordPress Page, Post, or Custom Post Type

Programmatically creates a WordPress post based on the incoming parameters.
/**
 * Programmatically creates a WordPress post based on the incoming parameters.
 *
 * Note: This function may need some additional work if you're dealing with non-English languages.
 *
 * @param	string	$title		The title of the page as presented to the users
 * @param	string	$slug		The slug used to access the page via the URL
 * @param	string	$post_type	The type of post to create. Can be either 'page' or 'post'
 * @param	string	$template	The template to apply to the page.
 * @return	int			The ID of the page that was created. -1 if the page already exists.
 */
function wp_snippets_create_post( $title, $slug, $post_type, $template = null ) {

	// Initialize the page ID so we know if it's been created
	$page_id = -1;

	// If a post doesn't already exist with the specified title, create it
	if( null == wp_snippets_get_permalink_by_title( $slug, $post_type ) ) {

		$page_id = wp_insert_post(
			array(
				'comment_status'	=>	'open',
				'ping_status'		=>	'open',
				'post_author'		=>	1,			// Administrator is creating the page
				'post_title'		=>	$title,
				'post_name'		=>	strtolower( $slug ),
				'post_status'		=>	'publish',
				'post_type'		=>	strtolower( $post_type )
			)
		);

		// If a template is specified in the function arguments, let's apply it
		if( null != $template ) {
			update_post_meta( get_the_ID(), '_wp_page_template', $template );
		} // end if

	} // end if

	return $page_id;

} // end wp_snippets_create_post

/**
 * Determine if a post or page already exists with the specified title.
 *
 * Note: This function may need some additional work if you're dealing with non-English languages.
 *
 * @param	string	$title		The title used to check if the post exists.
 * @param	string	$post_type	The post type that we're checking (could be post, page, or a custom post type)
 * @return	string				The permalink to the post. Null if the post or page doesn't exist.
 */
function wp_snippets_get_permalink_by_title( $title, $post_type ) {

    // Initialize the permalink value
    $permalink = null;

    // Try to get the page by the incoming title
    $post = get_page_by_title( strtolower( $title ), OBJECT, $post_type );

    // If the post exists, then let's get its permalink
    if( null != $post ) {
        $permalink = get_permalink( $post-&gt;ID );
    } // end if

    return $permalink;

} // end wp_snippets_get_permalink_by_title