- This topic has 17 replies, 2 voices, and was last updated 4 years, 3 months ago by
Tom.
-
AuthorPosts
-
February 8, 2019 at 2:37 am #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 headerRegarding (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.February 8, 2019 at 9:33 am #805230Tom
Lead DeveloperLead DeveloperHi 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 π
February 8, 2019 at 6:11 pm #805501culpable
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 paginationI’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 π
February 9, 2019 at 8:59 am #805998Tom
Lead DeveloperLead DeveloperA 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?February 9, 2019 at 6:15 pm #806301culpable
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 π
February 9, 2019 at 8:25 pm #806334Tom
Lead DeveloperLead DeveloperTry adding
<?php wp_head(); ?>
above<?php get_header(); ?>
.That might fix it.
February 9, 2019 at 8:40 pm #806338culpable
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 π
February 9, 2019 at 8:57 pm #806341culpable
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.
February 10, 2019 at 8:09 am #806683Tom
Lead DeveloperLead DeveloperIs there any way you can share the entire page template with me? Maybe through a pastebin?
February 10, 2019 at 3:11 pm #806910culpable
I can definitely share it through a paste bin if needed Tom βΒ but is email okay?
February 10, 2019 at 7:31 pm #806985Tom
Lead DeveloperLead DeveloperFor sure – you can use our contact form: https://generatepress.com/contact
February 11, 2019 at 9:46 am #807528Tom
Lead DeveloperLead DeveloperLet’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 π
February 12, 2019 at 3:59 am #808151culpable
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 placeFebruary 12, 2019 at 9:43 am #808539Tom
Lead DeveloperLead DeveloperHmm 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?
February 14, 2019 at 4:53 am #810164culpable
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
-
AuthorPosts
- You must be logged in to reply to this topic.