i am using this code to create a new page when my plugin is activated:
function create_policy() {
$my_post = array(
'post_title' => 'mobile page',
'post_content' => 'this is my content',
'comment_status' => 'closed',
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 3,4 )
);
wp_insert_post( $my_post );
}
register_activation_hook( __FILE__, 'create_policy' );
what i want to do is :
1) use my own code in the 'post_content', not regular text.
2) clear all the page and leave only the GET_header
i want a blank page with my code.
any advice ?
in addition to the comments:
this is the admin pannel code:
<?php
// create custom plugin settings menu
add_action('admin_menu', 'zeev_create_menu');
function zeev_create_menu() {
//create new top-level menu
add_menu_page('zeev movile redirect', 'zeev mobile', 'administrator', __FILE__, 'zeev_settings_page', 'favicon.ico');
//call register settings function
add_action( 'admin_init', 'register_mysettings' );
}
function register_mysettings() {
//register our settings
register_setting( 'zeev-settings-group', 'zeev_tracking_code' );
}
function zeev_settings_page() {
?>
<div class="wrap">
<form method="post" action="options.php">
<?php settings_fields('zeev-settings-group'); ?>
<table class="form-table">
<tr valign="top">
<td><textarea name="zeev_tracking_code" cols="90"><?php echo get_option('zeev_tracking_code'); ?></textarea></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php }
and this is the veriable what need to find himself on a blank page someware (and to work)
<?php echo get_option('zeev_tracking_code'); ?>
There are a few ways to get custom layouts in Wordpress on a page-by-page basis, but the most common is to create a new, custom template. In the theme folder, create a new file named what you like:
my-custom-template.php
The easiest way to start is to copy what you find in page.php into that directory. That will give you the structure with the headers and footers which are useful. Right at the top of the file, put:
<?php
/*
*
* Template Name: [My template]
*
*/
?>
Doing this will tell WP to automatically make this template an option when creating a new page. Now, when you create a page, you can select this template from the dropdown, and the page will use that. Inside the template itself, you can put whatever PHP or HTML code you need inbetween the get_header() and get_footer() you like, to style it, or just to show your two buttons.
If you want a totally blank page, you can remove the get_header() and get_footer() altogether.
Related
I have a custom post type with a few advanced custom fields. I'm trying to access these custom field values from within a Gutenberg block.
I've added the following to my register_post_type function
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'custom-fields' ),
I can successfully retrieve the custom posts from within my Gutenberg block using:
select('core').getEntityRecords('postType', 'customType')
but I'm not seeing the custom fields or their values.
Here's my block's JavaScript:
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { withSelect } = wp.data;
registerBlockType('cgb/block-press-block', {
title: __('Press Block'),
icon: 'awards',
category: 'common',
keywords: [
__('press-block'),
],
edit: withSelect((select) => {
return {
posts: select('core').getEntityRecords('postType', 'press')
};
})(({posts}) => {
return <p>Content</p>;
}),
});
Is there a way to access the custom post's advanced field values on the editor side or a way to pass that data to the block?
As you're using Advanced Custom Fields already, are you able to rather than registering your own block independently, use acf_register_block instead? That way you can access fields from ACF in PHP based templates.
Here are some useful links about this:
ACF 5.8 – Introducing ACF Blocks for Gutenberg
acf_register_block()
This code is taken from the ACF blog post above and posted here for completeness in case the above link changes.
Register the ACF block:
add_action('acf/init', 'my_acf_init');
function my_acf_init() {
// check function exists
if( function_exists('acf_register_block') ) {
// register a testimonial block
acf_register_block(array(
'name' => 'testimonial',
'title' => __('Testimonial'),
'description' => __('A custom testimonial block.'),
'render_callback' => 'my_acf_block_render_callback',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array( 'testimonial', 'quote' ),
));
}
}
A callback function to include your block template:
function my_acf_block_render_callback( $block ) {
// convert name ("acf/testimonial") into path friendly slug ("testimonial")
$slug = str_replace('acf/', '', $block['name']);
// include a template part from within the "template-parts/block" folder
if( file_exists( get_theme_file_path("/template-parts/block/content-{$slug}.php") ) ) {
include( get_theme_file_path("/template-parts/block/content-{$slug}.php") );
}
}
The HTML of your block:
<?php
/**
* Block Name: Testimonial
*
* This is the template that displays the testimonial block.
*/
// get image field (array)
$avatar = get_field('avatar');
// create id attribute for specific styling
$id = 'testimonial-' . $block['id'];
// create align class ("alignwide") from block setting ("wide")
$align_class = $block['align'] ? 'align' . $block['align'] : '';
?>
<blockquote id="<?php echo $id; ?>" class="testimonial <?php echo $align_class; ?>">
<p><?php the_field('testimonial'); ?></p>
<cite>
<img src="<?php echo $avatar['url']; ?>" alt="<?php echo $avatar['alt']; ?>" />
<span><?php the_field('author'); ?></span>
</cite>
</blockquote>
<style type="text/css">
#<?php echo $id; ?> {
background: <?php the_field('background_color'); ?>;
color: <?php the_field('text_color'); ?>;
}
</style>
This creates a basic testimonials block as a simple starting point. ACF handles the JavaScript handling within Gutenberg so all you have to do is worry about the PHP side of things.
That means you get to use get_field() and the_field() function like we're (ACF fans) are used to. Mixing ACF and Gutenberg without using this native way may cause headaches and possibly require a plugin to access fields via the WordPress REST API.
Note: ACF support for Gutenberg blocks requires ACF version 5.8 or higher.
I am working with a homemade theme, trying to keep it light weight, for a specific site. So far in development i am just using the index.php file to view pages and posts, both working smoothly and simple. I have a custom post type added, though, and when i try to open a single custom post, the index.php file loads, but nothing loads in the loop inside it. I guess that loops dont find any post at tall. My guess is something in the index.php or the register_post_type is going bananas. What do you think?
My index.php (slimmed):
<?php get_header(); ?>
<div class="hero-unit">
<?php get_template_part('hero'); ?>
</div>
<?php
// Start the loop.
while ( have_posts() ) : the_post();
the_content(); //disconnected the content just to see if i get response, but nothing appears
/*get_template_part( 'content', get_post_format() ); */
// End the loop.
endwhile;
?>
<?php get_template_part('featured'); ?>
</div><!-- /.container -->
My register_post_type in functions:
/* Custom Post Types */
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'kalender',
array(
'labels' => array(
'name' => __( 'Kalender' ),
'singular_name' => __( 'Kalenderpost' )
),
'supports' => array(
'title',
'editor',
'custom-fields',
'thumbnail'
),
'public' => true,
'has_archive' => true,
)
);
}
Any idea of what goes wrong? When i open a (WP-domain)/kalender/postname URL from the said single post in WP, it shows the full theme, except the content that should be found in the loop. If i try a trouble-shooting-loop with a 404 respons, it activates, like there are no matching post at all.
If you're using Apache server?
Make sure that rewrite module is enabled.
Also, check and see if .htaccess file has any issues or not.
When i use this inside of the loop on my bloglist page:
<?php comment_form( array( 'post_id' => get_the_ID() ) ); ?>
I get a form for each post wich is exactly what I want.
My problem is that since the form itself and the input fields and textarea has ids, the page won't validate. I get errors for multiple ids naturally.
For example does all my forms have id="commentform"
How can I make Wordpress remove all ids in these forms?
You can pass more options to the function see below. Replace <INSERT-UNIQUE-VALUE> with something unique or you can auto generate it with php for example md5(time());
<?php
// Specify options
$options = array(
'id_form' => '<INSERT-UNIQUE-VALUE>',
'id_submit' => '<INSERT-UNIQUE-VALUE>',
'comment_field' => '<p class="comment-form-comment"><label for="<INSERT-UNIQUE-VALUE>">' . _x( 'Comment', 'noun' ) . '</label><textarea id="<INSERT-UNIQUE-VALUE>" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>'
);
// output the form
comment_form($options, get_the_ID());
?>
For more references see: http://codex.wordpress.org/Function_Reference/comment_form#.24args
You could solve this with jquery by replacing your ID with a new ID and adding a number to your ids so the form id's would be id="newId0", id="newId1", id="newId2 and so on. Then it will validate.
$('form').each(function(index){
$(this).attr("id","newId"+index);
});
Here's a jsfiddle
Hi I'm looking for a very simple quick (no-plugins though) way to add a form inside a page template that will allow a logged-in user to post from outside WP-Admin. So basically the post will be under the logged-in author.
I have been looking around the web for some tutorials etc but haven't had too much luck and many seem to want to opt for plugins etc.
Can anyone help thanks.
You can use the wp_insert_post() function.
take a look at this. you can paste it in category.php for example and visit a category page and check it. but make sure to put the top code above the get_header() function.
<?php
if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];
$post_category = $_POST['cat'];
$post_content = $_POST['post_content'];
$new_post = array(
'ID' => '',
'post_author' => $user->ID,
'post_category' => array($post_category),
'post_content' => $post_content,
'post_title' => $post_title,
'post_status' => 'publish'
);
$post_id = wp_insert_post($new_post);
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect($post->guid);
}
?>
--
<!-- this form shows only if user is logged in -->
<?php if ( is_user_logged_in() ) { ?>
<form method="post" action="">
<input type="text" name="post_title" size="45" id="input-title"/>
<?php wp_dropdown_categories('orderby=name&hide_empty=0&exclude=1&hierarchical=1'); ?>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea>
<input type="hidden" name="new_post" value="1"/>
<input class="subput round" type="submit" name="submit" value="Post"/>
</form>
<?php } ?>
for more info you should take a look here wp_insert_post with a form
good luck
If you don't mind changing your theme, you might want to try P2: http://wordpress.org/extend/themes/p2
There's also a video of it in action here: http://p2theme.com/
P2 allows people to post straight from the home page, which seems like what your looking for.
I created a custom shortcode which includes a special loop which includes all posts from different multisite blogs. This solution is provided by this plugin: https://rudrastyh.com/. The shortcode is perfectly working an all normal pages and posts.
But I am also using the page builder Elementor. When inserting this shortcode into Elementor some strage things are happening: in editor mode the shortcode output is showing up twice, once at the top of the editor area and once again at the place where I actually put the shortcode. When I hit save, my whole site breaks and shows a standard image when accesing any page. Then the the only solution is to recover my latest database backup.
Here I show you some screenshots of the editor mode:
Here my shortcode fuction:
// Add Shortcode
function all_events_shortcode ($atts) {
// Attributes
$atts = shortcode_atts(
array(
'lang' => '',
'blog' => '',
),
$atts
);
// Network_Query parameters
$args = array(
'posts_per_page' => 14,
'blog_id' => esc_attr($atts ['blog']),
'lang' => esc_attr($atts ['lang']),
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_type' => 'noo_event',
'meta_key' => '_noo_event_start_date',
'meta_value' => date( "U" ),
'meta_compare' => '>'
);
$network_q = new Network_Query( $args );
// if there are posts, then print <ul>
if( $network_q->have_posts() ) :
echo '<div id="all_events">';
// run the loop
while( $network_q->have_posts() ) : $network_q->the_post();
// the get_permalink() function won't work without switch_to_blog()
// you can use network_get_permalink() instead but it is a little slower
switch_to_blog( $network_q->post->BLOG_ID );
// Get the dates
$start_date=get_post_meta($network_q->post->ID, '_noo_event_start_date', true);
$_start_date = gmdate("d.m.Y", $start_date);
$end_date=get_post_meta($network_q->post->ID, '_noo_event_end_date', true);
$_end_date = gmdate("d.m.Y", $end_date);
// you can obtain the post title from $network_q->post object
echo '<div class="all_events_item post-' . $network_q->post->ID . ' blog-' . $network_q->post->BLOG_ID . '">
<div class="all_events_img">
<a href="' . get_permalink( $network_q->post->ID ) . '">
'.get_the_post_thumbnail( $network_q->post->ID, 'large' ).'
</a>
</div>
<div class="all_events_content">
<h2>' . $network_q->post->post_title . '</h2>
<br />
<span class="start_date">'.$_start_date.'</span> -
<span class="end_date">'.$_end_date.'</span>
</div>
</div>';
// restore_current_blog() to switch to the previous (!) website
restore_current_blog();
endwhile;
echo '</div>';
endif;
network_reset_postdata(); // add it after the loop if you plan to use Network_Query multiple times on the page
}
add_shortcode('all-events', 'all_events_shortcode');
Can you give me some hints how I could tackle this problem?
Best wishes
Try this, it helped me.
Adding ob_start(); and then ob_get_clean();
function function_name(){
ob_start();
//..your code here...
$content = ob_get_clean();
return $content;
}
add_shortcode('shortcode_name','function_name');
Found the solution here and tried it. It worked for me.
Elementor page builder shortcode issue
Original credit: https://stackoverflow.com/a/48813883/9364624
Posted By: https://stackoverflow.com/users/1753934/colin-oakes
You need to bind HTML in variable and then return this HTML from shortcode..
Please check the code below
function _login_popup() {
$html = '<form id="user-login-form" method="post" action="#" class="js-form-redirect js-form-action" novalidate="">
<div class="floating-label form-group">
<input id="useremail" name="user_name" required="" value="" type="email">
<label for="useremail">Email</label>
</div>
<div class="floating-label form-group">
<input id="userpassword" name="password" required="" value="" type="password">
<label for="userpassword">Password</label>
</div>
<div class="o-form__buttons text-right --small-center">
<button type="submit" id="submit_login" class="a-button-form --save a-no-before" value="edit">Sign in</button>
</div>
</form>';
return $html;
}
add_shortcode('yg-login-popup', '_login_popup');
In this shotcode i have created login form..