• Hi, i have a collection of pages that i would like to all have the same appearance – a members profile page, containing a picture, some blurb, and a table of some data. I tried using a gutenberg block for this, but it’s not quite working how i need it to. I’ve created the resuable block ok (which is a combined media block and table block), but when entered into a profile page, the user has to basically edit the actual block in order to fill it in. So, to add a picture, it actually adds the picture to the saved block. So the next users page has the previous users image in it. I was hoping it would be like a template, that i can copy into a page and the user edits on a per-page basis. Or maybe i’m using it wrong?

Viewing 11 replies - 16 through 26 (of 26 total)
  • Thread Starter ts_hamlet

    (@ts_hamlet)

    Ok, so i added the following rewrite in my child theme:

    add_action('init', 'add_rewrite_member_page');
    function add_rewrite_member_page() {
        add_rewrite_tag('%member%', '([^&]+)', 'member=');
        add_rewrite_rule('^goto/([^/]*)/([^/]*)/?','index.php?member=$matches[1]&pagename=$matches[2]','top');
    }

    ..and have gone to the permalinks page, saved (flushed?) it.

    Created a file in my themes page-templates directory called page-view-member.php, with just the following to see if it’s working:

    <?php get_header(); ?>
    			<?php while ( have_posts() ) : the_post(); ?>
    
    				<?php
    					print_r($_GET['member']);
    				?>
    
    				<?php thinkup_input_nav( 'nav-below' ); ?>
    
    				<?php thinkup_input_allowcomments(); ?>
    
    			<?php endwhile; wp_reset_postdata(); ?>
    
    <?php get_footer(); ?>

    …but i’m unsure of the url to load this template?

    Thread Starter ts_hamlet

    (@ts_hamlet)

    @jummi1 I’m not sure about the ACF, i have only heard about the plugin from this topic, but reading the documentation it seems they are capable of much more. I am using the free version in my site, but so far i only need quite basic functionality.

    Most of what i learned in this thread has been patiently explained by @bcworkz, but creating a template is as simple as popping one in the page-templates folder of your theme using the naming convention defined in the wp documentation. I haven’t come across a specific ACF template tutorial either, so another hat-tip to bcworkz for explaining how to do this here.

    @ts_hamlet Yes I also have php templates for various pages. But what I really wanted was “micro” templates, which would themselves be used on various landing pages. Basically, custom blocks / widgets that can be pulled in where necessary. I’m not sure that can be done as you’ve said?

    Regardless, I found another two ways.

    One is to have a “reusable block” and simply “convert” it to regular blocks each time it’s used. Ugly but works. You must place your blocks in a “group” block, and save that as the reusable block.

    Another is to create a “custom block”. I didn’t want to get into that because I prefer to avoid php/react, but turns out it’s quite simple. That’s the route I’m taking and the result is a proper “template” widget. What also helps is that there are countless custom blocks on the wordpress marketplace, so you can look at the source for any of them.

    The gold standard I think though, is to use ACF or one of the two open source alternatives (“Carbon Fields” or “CMB2”). But I’m worried about the learning curve – I just want to get stuff done, that’s why I’m trying to move my stuff from “proper” platforms to WordPress.

    Moderator bcworkz

    (@bcworkz)

    The good thing (or bad depending on your goals) about the ACF style of adding content is it enforces a rigid structure defined by the template using the fields. Reusable blocks OTOH are good for repetitive content. But if you desire to enforce a “micro-structure” of content that is rigid within, but can be freely used where an author desires and contains variable content, you’re talking about custom editor blocks.

    If the learning curve for ACF concerns you, you are not going to like custom blocks. But if the sort of work you do requires this sort of thing on a regular basis, it’s probably worth learning about.
    https://developer.www.ads-software.com/block-editor/tutorials/block-tutorial/

    @bcworkz I spent a day with custom blocks and it wasn’t too bad. Already made a few of them.

    Basically I have landing pages, which are almost all the same. I want to be able to drop various pre-built “sections” in each one, and reorder them, maybe change the wording, etc. Custom blocks allows me to do this, but I forsee it being a nuisance to maintain. Something easier would be preferable.

    I didn’t realize ACF was EASIER – thanks for telling me. I’ll definitely look into it now!

    Thread Starter ts_hamlet

    (@ts_hamlet)

    So i saw an error in my rewrite rule. The url i’m looking for is mysite.com/view-member/john-doe. My updated rule is:

    add_action('init', 'add_rewrite_member_page');
    function add_rewrite_member_page() {
        add_rewrite_tag('%member%', '([^&]+)', 'member=');
        add_rewrite_rule('^view-member/([^/]*)/([^/]*)/?','index.php?member=$matches[1]&pagename=$matches[2]','top');
    }

    But i’m still getting a (wp) 404 page?

    Moderator bcworkz

    (@bcworkz)

    You are only matching a 3 element permalink as in example.info/view-member/john-doe/my_page/. Drop the last element. Do you have code to tell WP what to do with “member” query var? If not, assign the permalink element to “author_name” query_var. Then you don’t really need the rewrite tag though it doesn’t hurt anything to have it.
    add_rewrite_rule('^view-member/([^/]*)/?','index.php?author_name=$matches[1]','top');

    Don’t forget to visit the permalinks settings screen after making rewrite changes ??

    Thread Starter ts_hamlet

    (@ts_hamlet)

    Ok, so i have just this in my child themes functions.php referring to the rewrite:

    add_action('init', 'add_rewrite_member_page');
    function add_rewrite_member_page() {
        add_rewrite_tag('%member%', '([^&]+)', 'member=');
        add_rewrite_rule('^view-member/([^/]*)/?','index.php?member=$matches[1]','top');
    }

    and the file /child-theme/templates/page-view-member.php.

    Using the url mysite.com/view-member/john-doe, i get redirected to the home page.

    I found using index.php?author_name= returned a 404, but using index.php?author= took me to the author archives page – i’m trying to display the custom page in the above templates dir. If easier, i could overwrite the archive page content, but would prefer to display a custom page that just shows the ACF fields.

    Moderator bcworkz

    (@bcworkz)

    Yes, author not author_name. I forgot WP does a conversion from query string in URL to query var in WP_Query. Such requests go to author.php if it exists, otherwise one of the archive templates are used. If you don’t want to rename your template file use “template_include” filter to override the template WP chooses by default.
    https://developer.www.ads-software.com/reference/hooks/template_include/

    Aaack, I just discovered that the author bit only works if the user has actually authored posts. If that’s a problem, you should go back to the “member” query var and use the “pre_get_posts” action to check for the query var and alter the WQP_Query object accordingly to so only their posts are queried for. Here you would set author_name.

    Thread Starter ts_hamlet

    (@ts_hamlet)

    Yes the users on this page aren’t authors or anything, they don’t write articles on the site. As it stands, each user has a page that they can edit / are the author of. I’m looking to move away from that and just display a generic page for each user that displays the ACF fields.
    So, if they are the author of their own page, then that should be that issue side-stepped.
    template_include looks promising, but i’m just getting a blank page:

    
    add_filter( 'template_include', 'view-member', 99 );
    function portfolio_page_template( $template ) {
    	if ( is_page( 'archive' )  ) {
    		$new_template = locate_template( array( 'view-member.php' ) );
    		if ( '' != $new_template ) {
    			return $new_template ;
    		}
    	}
    }

    …with the page view-member.php in my child theme root dir. Also note i’m checking if the current page is “archive” (since that’s what it was displaying).

    • This reply was modified 4 years, 11 months ago by ts_hamlet.
    Moderator bcworkz

    (@bcworkz)

    You need to return the passed value $template at the very end, outside of conditionals, for when there is no archive request.

    	\\...
    		if ( '' != $new_template ) {
    			return $new_template ;
    		}
    	}
    	return $template;
    }

    Filter callbacks must always return something.

    ETA: Just noticed the add_action() call is using the wrong function name. Do this:
    add_filter( 'template_include', 'portfolio_page_template', 99 );

    • This reply was modified 4 years, 11 months ago by bcworkz.
Viewing 11 replies - 16 through 26 (of 26 total)
  • The topic ‘“Static” / template-type reusable blocks’ is closed to new replies.