I'm working on a site for a client that runs a social ransom style blog. This is where the user has to click one of the social media buttons to get access to the content.
However, she is now going to use the OnePress Social Locker Plugin for wordpress which works by wrapping the content in specific tags, like so:
[sociallocker] Content Here will Be Locked [/sociallocker]
At the moment she already has her own custom shortcode in place which works like this:
[socialLock url="http://example.com" text="Click here to view content"] Content here will be locked [/socialLock]
The problem is that she has over 2,300 pages on her blog and to change every single one indv. through the dashboard will take ages and this is not a by the hour pay contract.
I thought that I would be able to pass the current shortcode into the new one like so:
function parseShortcode_func( $atts ) {
$atts = shortcode_atts( array(
'link' => '',
'text' => 'default text'
), $atts );
return "[sociallocker] {$atts['text']} [/sociallocker]";
}
add_shortcode( 'socialLock', 'parseShortcode_func' );
However that just outputs
[sociallocker] Click here to view content [/sociallocker]
Has anyone got any ideas how to parse this second shortcode within a shortcode?
The do_shortcode() function will reparse your output string.
return do_shortcode("[sociallocker] {$atts['text']} [/sociallocker]");
Related
Using a WordPress multisite network, I need to access custom post types from our main site from within a shortcode.
For example, our main site (ID 1) is where the custom post types (Case Studies) are stored. In functions.php I have the following:
//[casestudy]
add_shortcode( 'casestudy', 'casestudy_shortcode' );
function casestudy_shortcode( $atts ) {
$a = shortcode_atts( array(
'id' => ''
), $atts );
switch_to_blog(1);
//Get fields from custom post type with Advanced Custom Fields Pro
//and return HTML output with them
restore_current_blog();
}
Then call the shortcode with [casestudy id="123"] where the ID is the post ID of the case study.
The problem is, doing this returns the Case Study HTML fine but breaks some features of the page and also populates the 'recent posts' widget with blog posts of the main site.
Any ideas on what's going wrong? Thanks.
Adding my comment as an answer:
Reading the OP code comments, it looks like restore_current_blog() is never called, because the HTML is returned before calling it.
Make sure that the return part is the last line in there.
I am creating a custom WordPress plugin based on Boilerplate generator, and work fine. The only thing issue that to every frontend page that i display data from the plugin through a shortcode, at the end of the page, i get the number 1.
It seems that something produced that number but i searched all the files line by line and i cannot find what is producing it.
My hands up...i really cannot find what causes this 1 to be in each public page.
I suspect that it may is produced by
$this->loader->add_action( 'test-plugin', $plugin_public, 'display_front_page' );
add_shortcode( 'test-plugin', array( $plugin_public, 'display_front_page') );
This produces the shortcode [test-plugin] that it displayes the data from the function display_front_page
Any idea what causes that 1 to be displayed at the end of the page??
The title might not be completely clear, but I didn't know how to ask this in another way.
I want to build a system in Wordpress where the user can put some projects together where it would be on an url like http://mywordpress.com/projectbuilder/ or something like that.
Normally I would create an page in the admin menu and set it to a certain template and in the content I would put some text like: "Do not delete this page, this content is not shown".
But I think there must be a better way to add a custom page to a certain URL without adding it in the backend as a page with "useless content" since the content would not be changeable from the backend in this case.
I hope this makes sense. How could I go about that?
I think I could achieve this with a custom plugin but I can't seem to find any code how to go about that. I have found a way to add administration pages in the settings menu on the right. But I want to add a page to the website on the front end.
Sorry i didn't got your question properly. but some what say to create Custom post or taxonomy :
Please check below link
Custom Post and Taxonomies
In your functions.php file add this anywhere:
function themeslug_projects() {
$args = array(
'public' => true,
'label' => 'Projects',
'rewrite' => array( 'slug' => 'projects' ),
);
register_post_type( 'projects', $args );
}
add_action( 'init', 'themeslug_projects' );
Do save your permalink settings again after doing this, this will work surely then..
basically you can do this by creating a rewrite rule and then point to a file.
add_action('init', 'add_rewrite_rule');
function add_rewrite_rule(){
// add_rewrite_rule(REGEX url, location, priority (i.e. top is before other rewrite rules)
//basically tell wordress to add a query var if sidebar is added to url. change sidebar to what you want your link to be.
// i set up a custom post type to make this work called custompostype..it does nothing but just to give post_type a value.
add_rewrite_rule('^sidebar?','index.php?is_sidebar_page=1&post_type=customposttype','top');
}
// register a query var
add_action('query_vars','market_set_query_var');
function market_set_query_var($vars) {
array_push($vars, 'is_sidebar_page');
return $vars;
}
// associate a template with your quer_var
add_filter('template_include', 'market_include_template', 1000, 1);
function market_include_template($template){
if(get_query_var('is_sidebar_page')){
$new_template = (theme or plugin path).'/pages/yourpage.php'; // change this path to your file
if(file_exists($new_template))
$template = $new_template;
}
return $template;
}
after adding any rewrite rule, the changes wont take place until you go into settings->permalinks and hit the "save" button.
The theme I am using has a page called 'home' whos body is displayed on the home page. I have another page set up called 'about' which is a detailed about page and quite long. I want there to be text on the homepage that is a short synopsis of the about page. I have this synopsis in the excerpt of the 'about' page. Is there a way for me to use that excerpt in the body of the 'home' page?
By doing it this way I can tell the client that all the about information is in one page instead of telling him to go to different pages to edit things that are directly related.
edit: I want to do it without editing the themes code so through the admins page editor
Rather than using "include", you could use the following to get the page excerpt of the about page and stick with native wordpress functionality:
$about = get_page_by_title( 'ABOUT_TITLE' );
$about_excerpt = $about->post_excerpt;
Then you can echo it into your theme template using:
<?php echo $about_excerpt; ?>
If you wanted to do this within the Wordpress backend, you would create a custom shortcode and wrap the above into a function to be called by that shortcode. Then you could put that shortcode wherever you wanted on any post/page.
EDIT:
Here is an example of doing this via shortcode so that you can use this for the About page or any other page with an excerpt within Wordpress:
add_shortcode( "Excerpt", 'nb_excerpt_shortcode' );
function nb_excerpt_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'for' => ''
), $atts) );
$page = get_page_by_title( $for );
$excerpt = $page->post_excerpt;
return $excerpt;
}
Then call it into action with [Excerpt for="About" /]
It sounds like something as simple as a PHP include of your 'about' file in your home file would give you what you are looking for ...
Place this code ( or an excerpt thereof ) in your home file, and make sure that your 'about.php' file is in the same folder...
<body>
<div id="about">
<?php include("about.php"); ?>
</div>
</body>
This would allow you to make changes to your about file without directly affecting your home, but the changes would be applied to both pages...
how do I programically create a page in WordPress if it doesn’t exist already?
I want to write a plugin and to put some html controls in a page which will automatically create when user install the plug in
Based on that comment, you want to hook a function to your plugin's activation hook, which inserts a WordPress post object into the database;
function my_plugin_activate()
{
wp_insert_post(array(
'post_type' => 'page',
'post_title' => 'Page Title',
'post_content' => 'Page Content',
'post_name' => 'page-slug',
));
}
register_activation_hook(__FILE__, 'my_plugin_activate');
How will you identify that page? Assuming you have a specific title, use something like
if( get_page_by_title('my_title') === false ) // page doesn't exist
{
// insert the page using wp_insert_post
}
If you need help with wp_insert_post, please comment.
You can implement a function that intercepts the template_redirect action/filter, and inside that function include the theme's header and footer, while creating your own content to put into the body of the page. See the API link (above) for an example.
Here's a tutorial on the web.