[Support request] How To Insert Code Into Header In Custom PHP Page

Home Forums Support How To Insert Code Into Header In Custom PHP Page

This topic contains 17 replies, has 2 voices, and was last updated by  Tom 6 days, 14 hours ago.

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #804751

    culpable

    Hiya,

    In the link attached, I’m currently using a custom coded PHP page to generate my HTML page.

    I am using the <?php get_sidebar();?>, <?php get_footer();?>, and <?php get_header();> to mimic the layout of the rest of my Generatepress site.

    Everything has been all fine and dandy until I realised that:
    (a) the canonical URL for these pages is the home page
    (b) I am unable to insert other code into the header

    Regarding (b), I want to remove the existing canonical URL to the home page. I also want to insert php code into the header which will handle the canonicals for pagination (i.e. rel=next and rel=prev).

    What is the best way for me to go about doing this?

    Thank you very much for your help.

    Note: The pagination is broken on this page specifically because the the rel links are not inserting in the header, instead inserting just under the <div id="content" class="site-content"> in the html.

    GP Premium 1.7.7
    #805230

    Tom Lead Developer

    Hi there,

    Is there any reason you create a custom page template to this? It seems like something that should be achievable with the default template.

    Let me know πŸ™‚

    #805501

    culpable

    It was transferred over from a non-wordpress site, and this seemed like the easiest way to get everything over (or rather – the only way a non-wordpress developer [not I] could figure out how to do it).

    The page itself is a PHP file which:
    – Establishes a database connection
    – Pulls questions from the DB based on the subject, and generates pages of each one
    – Uses <?php include> to include other functions that handle things like pagination

    I’m decent with WordPress, but no where near your superstar level. I can kind of see how this would be possible using a standard wordpress page.

    The way I would go about doing it though is turning essentially this entire PHP file into a shortcode, then executing that shortcode on a page? But even then it’s a bit confusing for me.

    For instance, how would I ensure that the pages generated from these database queries would become their own “WordPress page”?

    Essentially I’m open to anything that works πŸ™‚

    #805998

    Tom Lead Developer

    A shortcode was my first thought as well, but it depends on what exactly your content is doing.

    You should try this:

    add_shortcode( 'my-shortcode', function() {
        ob_start();
    
        ?>
            Your page content in here
        <?php
    
        return ob_get_clean();
    } );

    Otherwise, does your current page template have wp_head() added to the header?

    #806301

    culpable

    Right – so make a new page and run that shortcode on the page?

    I’d imagine I’d also have to delete the old PHP script to avoid URL conflicts?

    To answer your other question: no. The only calls it has to external resources are:

    <?php require( $_SERVER['DOCUMENT_ROOT'] .'/wp-load.php' );?> (I’m not sure what this does :|)
    <?php get_header();?>
    <?php get_sidebar();?>
    <?php get_footer();?>

    Thanks Tom πŸ™‚

    #806334

    Tom Lead Developer

    Try adding <?php wp_head(); ?> above <?php get_header(); ?>.

    That might fix it.

    #806338

    culpable

    Adding this code seems to make the header take up the entire viewport height. The canonical remains too.

    I’ll try out the shortcode method and report back πŸ™‚

    #806341

    culpable

    Unfortunately, it doesn’t seem to be working. The code did not properly execute to create all of the required pages (can I wordpress page even do that? Execute code that creates other pages? And even if it did – wouldn’t those pages still have to include code like the <?php get_header();?> to ensure they are correctly formatted?).

    Going back to using the PHP file, how would one:

    – Call the WordPress header without inheriting the strange homepage canonical
    – Insert other code into this WordPress header (in this case, new canonical urls in the <head>)

    Since the <?php get_header(); ?> includes both the stuff in the <head> plus the other stuff in the header like navigation – is there a way for me to call the parts of this function? Then maybe replace the <head> with that I need?

    Or maybe use a function to just call the <head>, then use PHP code to remove the </head> at the end and insert my own code (which includes the </head> at the end?

    Still though, I would need to remove this weird canonical to the home page…

    Last idea: do something cheeky with the “Elements” to set a display rule to NOT “everything else on the site”… thereby only displaying on this page? Code generated via this script is the only non-wordpress page/post on the site.

    #806683

    Tom Lead Developer

    Is there any way you can share the entire page template with me? Maybe through a pastebin?

    #806910

    culpable

    I can definitely share it through a paste bin if needed Tom – but is email okay?

    #806985

    Tom Lead Developer
    #807528

    Tom Lead Developer

    Let’s start by trying to fix the canonical URL issue.

    Try this function:

    add_action( 'wp', function() {
        if ( is_page( 10 ) ) {
            remove_action( 'wp_head', 'rel_canonical' );
        }
    } );

    You’ll need to update the 10 to the ID of your page.

    Let me know if that works or not πŸ™‚

    #808151

    culpable

    That’s a great idea Tom.

    Only issue in this case is that since it’s not strictly a “wordpress page” – I’m not sure how to obtain the page’s page ID (or, if it even has a page ID).

    These pages are generated via the PHP file I sent over.

    Maybe a way to strip the canonical from all pages on the site that aren’t wordpress pages/posts? I’m not actually sure why the <?php get_header(); ?> function is pulling in this homepage canonical in the first place

    #808539

    Tom Lead Developer

    Hmm you’re right, there isn’t currently a way to target the page, as it’s not even a page template: https://developer.wordpress.org/themes/template-files-section/page-template-files/

    I wonder if creating it as a page template is a possibility?

    #810164

    culpable

    Sorry for the late reply Tom – trying to get my head around page templates.

    I think I understand how it might be coded. A couple of questions:

    1. If I am essentially building the php of the page template from scratch, then what benefit do page templates offer me in terms of overcoming my issue? As in, what does this allow for that I can not currently do with my current setup? To me, it just seems like these page templates would streamline the creation of new content built using the same structure as the page template. But wouldn’t I still be using <?php get_header();?>, <?php get_sidebar();?>, <?php get_footer();?>, etc? So wouldn’t I have the same issue I’m having now with my inability to control canonical tags (as I’m still calling the same functions)?

    2. How would I deal with pagination? Would each page of the pagination be considered a separate “wordpress page” that I need to create a new wordpress page for? Or would I hard code the “subject” for each page (with each page based on the page template) and pull only that subject from the database? Essentially I’m not sure how pagination is handled in wordpress pages.

    I hope I articulated my questions okay. Thank you for your patience Tom

Viewing 15 posts - 1 through 15 (of 18 total)

You must be logged in to reply to this topic.