How to Access WordPress and WooCommerce Page Information Using PHP

October 21, 2023 (1y ago)

10,033 views

When building WordPress and WooCommerce sites, I often need to retrieve various pieces of information like IDs, titles, and URLs for different system pages—think Blog, My Account, Shop, Cart, and Checkout. But first, let's not overlook the simple yet foundational elements: the static pages. Whether it's your main landing page or a dedicated blog section, these pages form the core of many WordPress sites.

woo commerce info php 1

All this data is neatly stored in the *_options table, and today, I'll show you how to access it using some handy WordPress functions.

A Peek at the *_options Table

WordPress cleverly uses this table to store settings and configurations. This includes the IDs for your static pages, whether they're set as your front page or blog page. Here’s a little bit about how WordPress handles these:

  • Static Front Page: This is the main landing page of your site. WordPress stores its ID in the page_on_front option.
  • Static Blog Page: If you have a separate page for your blog posts, its ID is stored in the page_for_posts option.
  • WooCommerce extends this to its essential pages, storing IDs for My Account, Shop, Cart, and Checkout.

Fetching Page Details in WordPress

Here's a straightforward guide on how you can access the IDs and use them to fetch the titles and URLs of static WordPress pages:

For the Static Front Page

Suppose you've set a specific page as your homepage in WordPress settings:

/* GET URL */
function get_front_page_url() {
    return get_permalink(get_option('page_on_front'));
}
/* GET TITLE */
function get_front_page_title() {
    return get_the_title(get_option('page_on_front'));
}

And the Static Blog Page

If you have a separate page for your blog posts:

/* GET URL - Direct Method */
function get_blog_page_url() {
    return get_permalink(get_option('page_for_posts'));
}
/* GET URL - Using Post Type Archive Link */
function get_blog_page_url2($type = 'post') {
    return get_post_type_archive_link($type);
}
/* GET TITLE */
function get_blog_page_title() {
    return get_the_title(get_option('page_for_posts'));
}

Working with WooCommerce Pages

Similarly, WooCommerce keeps its page IDs in the same table. Here’s how to retrieve them and get more info about the pages.

My Account Page

/* GET URL - Using WooCommerce Function */
function get_my_account_page_url() {
    return get_permalink(get_option('woocommerce_myaccount_page_id'));
}
/* GET URL - Direct WooCommerce Helper */
function get_my_account_page_url2() {
    return get_permalink(wc_get_page_id('myaccount'));
}
/* GET URL - Using WooCommerce Permalink Getter */
function get_my_account_page_url3() {
    return wc_get_page_permalink('myaccount');
/* GET TITLE */
function get_my_account_page_title() {
    return get_the_title(get_option('woocommerce_myaccount_page_id'));
}

Shop Page

Here's how you can fetch the URL and title for the WooCommerce Shop page:

/* GET URL - Multiple Methods Demonstrated */
function get_shop_page_url() {
    return get_permalink(get_option('woocommerce_shop_page_id'));
}
function get_shop_page_url2() {
    return get_permalink(wc_get_page_id('shop'));

function get_shop_page_url3() {
    return wc_get_page_permalink('shop');
/* GET TITLE */
function get_shop_page_title() {
    return get_the_title(get_option('woocommerce_shop_page_id'));
}

Cart and Checkout Pages

The process to access the Cart and Checkout pages is quite similar:

/* GET URL - Checkout Page */
function get_checkout_page_url() {
    return get_permalink(get_option('woocommerce_checkout_page_id'));
}
function get_checkout_page_url4() {
    return wc_get_checkout_url();
/* GET TITLE - Checkout Page */
function get_checkout_page_title() {
    return get_the_title(get_option('woocommerce_checkout_page_id'));
}

Handling Plugin-Specific Pages

Even plugins like Yith WooCommerce Wishlist store their page information in the same way:

/* GET URL - Wishlist Page */
function get_yith_wishlist_page_url() {
    return get_permalink(get_option('yith_wcwl_wishlist_page_id'));
}
/* GET TITLE - Wishlist Page */
function get_yith_wishlist_page_title() {
    return get_the_title(get_option('yith_wcwl_wishlist_page_id'));
}

Wrap Up

That’s a wrap on how to fetch IDs, titles, and URLs for key WordPress and WooCommerce pages using PHP. This method allows for dynamic referencing of page information which is super useful for plugin development, custom themes, or when you're tweaking functions. If you're curious or need more customization, don't hesitate to dive into the WordPress and WooCommerce core files or peek into the database for deeper insights.