I would like show the admin notice while post text is being edited (before Save post). I suppose admin_notice hook wont work in this (it doesn't for me). Any idea how to show some error message to alert the user that Post content is not going to be accepted ?
I prefer inline display like admin notice instead of popups that could be blocked.
Following doesn't work
function secure_oembed_filter($html, $url, $attr, $post_ID) {
if (strlen($html) > 0 && strpos($html, "http://")) {
//wp_die("Unsecure link in embeds", "Unsecured post");
} else {
add_action('admin_notices', array($this, 'show_error'));
}
return $html;
}
private function show_error () {
echo '<div class="error"><p>This is an error</p></div>';
}
In that case wp_remote_get() will work fine.
Create a notice.php file and uploaded it on my server.
Then I have added these code into my plugin and it works for me.
<?php
$remote_data = wp_remote_get( 'http://example.com/notice.php' );
$remote_body = wp_remote_retrieve_datat( $remote_data );
?>
<div class="notice">
<?php echo $remote_body ?>
</div>
Hope that helps.
Related
I want to send an email with contactform 7 dynamic hidden field wordpress plugin, to get dynamic content to the email.
This is possible while using a shortcode. So I wrote the shortcode and the function, and it seems like it could work, because on the website, the correct output is displayed, but it doesn't send it with the mail. I need to get content from several posts and custom fields by ID displayed as list.
It sends the proper content when there is a simple return 'random text';
But it doesn't send anything with echo for example.
So how can I get the content created by the function in a way, that it is a simple return, that can be sent?
function show_list_function() {
if(!empty($_SESSION['acts'])){
foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
echo get_the_title($actID);
the_field('lange', $actID);
}
} else {
return 'Nothing selected'; //this is working
}
}
add_shortcode( 'show_list', 'show_list_function' );
Thanks for any help and tips!
Shortcode output can't be echo'd out, it must be returned, since do_shortcode is used by echo do_shortcode()
From the codex:
Note that the function called by the shortcode should never produce an output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results.
function show_list_function() {
// Init $output as something
$output = '';
if(!empty($_SESSION['acts'])){
foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
// concatenate the $output string
$output .= get_the_title($actID);
$output .= get_field('lange', $actID);
}
} else {
$output = 'Nothing selected'; //this is working
}
return $output;
}
add_shortcode( 'show_list', 'show_list_function' );
You can use ob_start() and ob_get_clen();
function show_list_function() {
ob_start();
if(!empty($_SESSION['acts'])){
foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
echo get_the_title($actID);
the_field('lange', $actID);
}
} else {
echo 'Nothing selected'; //this is working
}
$html = ob_get_clean();
return $html;
}
add_shortcode( 'show_list', 'show_list_function' );
I am trying to build a wordpress-like plugin for my own CMS using str_replace. So when user saves something like '[do_something]' into page content, it will be replaced by a prebuild function.
example:
<?php function do_something() {
// search database and display something
}
<?php
echo str_replace("[do_something]","<?php do_something(); ?>",$page_content);
?>
However using echo it returns
"<?php do_something(); ?>"
as text instead of php script and fails to execute.
Any suggestion?
UPDATED
So finally, I have come up with this:
function plugin($page_content){
$plugins = array(
'[function1]' => 'function1();',
'[function2]' => 'fucntion2();'
);
foreach($plugins as $plugin => $function) {
if (strpos($page_content, $plugin) !== false) {
eval($function);
break;
}
}
}
:)
Try this:
<?php
eval(str_replace('[do_something]', 'do_something();', $page_content));
?>
But please remember that using eval especially with user's input is a very bad practise.
I have some trouble coding a themes shortcode. I want the code to display a div with a view counter function, and then a link with the shotcodes content as the url.
The view_count(); function works fine when called inside theme files, and I actually managed to make it show, but then it displayed before the_content(); of the post (the gray bar), when I wanted it within content under an element.
(1) Here's what I've got:
function video_block( $atts, $content = null ) {
return '<div class="video-block"><span class="ViewCount"><?php the_views(); ?></span> <a class="dl-source" href="' . $content . '">Link</a></div>';
}
(2) Here's the code that displays both on top of page:
function video_block( $atts, $content = null ) { ?>
<div class="video-block"><span class="ViewCount"><?php the_views(); ?></span> <a class="dl-source" href="<?php echo $content; ?>">Link</a></div>
<?php }
(3) This code displays the views above content, and the link in the correct place:
function video_block( $atts, $content = null ) {
$views = the_views();
return '<div class="video-block"><span class="ViewCount"><?php $views; ?></span> <a class="dl-source" href="<?php echo $content; ?>">Link</a></div>';
}
I read somewhere in the Wordpress forums that you should return (instead of echo) values within functions, but that breaks it, displaying the view count, skips the html and spits out the $content.
Here is a link to the page in question: http://nvrt.me/4Qf1 (currently using code from block #2)
I'm running out of midnight oil. I would really appreciate it if someone could help me out.
EDIT:
Here is the code for the_views(); function. I can see that it's echoed, but when changed to return, it doesn't display it at all.
### Function: Display The Post Views
function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
$post_views = intval(post_custom('views'));
$views_options = get_option('views_options');
if ($always || should_views_be_displayed($views_options)) {
$output = $prefix.str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $views_options['template']).$postfix;
if($display) {
echo apply_filters('the_views', $output);
} else {
return apply_filters('the_views', $output);
}
}
elseif (!$display) {
return '';
}
}
Even though it is a recommended practice in Wordpress to have functions that return values, it is not always possible, especially when you are calling another function that writes it's output directly to the stream.
When the component writes it's output directly to the stream you need to code to accommodate that behavior unless you want to rewrite the who component (I wouldn't do that ;-) ).
In this instance the the_views() function actually offers you both options. If you look at the $display parameter and follow the code this function can behave both ways. If $display is set to True (it's default) it will echo the results of the function. If $display is set to False it will return the output.
So you have two options, both of which should work:
Option 1, Return a value
Note that when I call the_views() I pass it a false parameter like this: the_views(false)
<?php
function video_block( $atts, $content = null ) {
return "<div class=\"video-block\"><span class=\"ViewCount\">" . the_views(false) . "</span><a class=\"dl-source\" href=\"$content\">Link</a></div>";
}
?>
*Option 2: Echo your output *
Note that when I call the the_views() there are no parameters passed to it.
<?php
function video_block( $atts, $content = null ) {
echo "<div class=\"video-block\"><span class=\"ViewCount\">";
the_views();
echo "</span><a class=\"dl-source\" href=\"$content\">Link</a></div>";
}
?>
Oh, also, don't forget to escape your quotes when you are returning a string.
Couldn't add the code block as a comment on #BLewis response but working off what he said, I did something like this:
function my_shortcode() {
ob_start();
a_function_to_display();
$data = ob_get_clean();
return $data;
}
add_shortcode( 'shortcode_name', 'my_shortcode' );
I'd like to contribute a new answer here.
I was working on extending a plugin and found this was how the author did it and to me it makes much more sense.
If you're outputting a lot of HTML you should not use echo, it's bad practise, hard to read and a pain to keep the formatting of the output nice using echo. Even if you use it's counter parts, it's still less clean to have echo_r or print or var_dump than to use pure HTML.
Since PHP only runs inside the wrapper tags <?php and ?>, you should use this to your advantage.
But since we can't just do this here because you may lose your content order, you can use some object storing. Basically before you start your HTML block (probably at the beginning of your shortcode function would be good), just add ob_start(); to store your code. Then just before ending your function, you can return your data using $data = ob_get_clean(); and return $data; .
Good luck with more WordPress work.
I've added a custom "save_post" action to my theme (code is below). However, when I place images or video code in the post, its stripped away. The only way I can get it to stay is to comment out the add_action line. What do I need to do in order to keep all the post info intact?
add_action('save_post', 'custom_add_save');
function custom_add_save($postID){
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $postID;
}
else
{
// called after a post or page is saved
if($parent_id = wp_is_post_revision($postID))
{
$postID = $parent_id;
}
if ($_POST['my_customHeader'])
{
update_custom_meta($postID, $_POST['my_customHeader'], 'my_customHeader');
}
else
{
update_custom_meta($postID, '', 'my_customHeader');
}
if ($_POST['my_customTitle'])
{
update_custom_meta($postID, $_POST['my_customTitle'], 'my_customTitle');
}
else
{
update_custom_meta($postID, '', 'my_customTitle');
}
}
}
function update_custom_meta($postID, $newvalue, $field_name) {
// To create new meta
if(!get_post_meta($postID, $field_name)){
add_post_meta($postID, $field_name, $newvalue);
}else{
// or to update existing meta
update_post_meta($postID, $field_name, $newvalue);
}
}
you also need to add a nonce value to prevent concurrency
add a hidden input in your form:
<input type="hidden" name="customCategory_noncename" id="customCategory_noncename" value="<?= wp_create_nonce('customCategory'); ?>" />
and add this to your save code
// verify this with nonce because save_post can be triggered at other times
if (!wp_verify_nonce($_POST['_noncename'], 'my_customHeader')) return $post_id;
also by default i think wordpress strips out html formatting in the editor in favour of its own 'smart' html tagging
I'm not exactly very well-versed with the Wordpress hooks related to saving posts, but based on your PHP alone, I'm seeing that your custom_add_save() function is not returning the post id when it's processing a manual save (i.e. when you click the Save Draft / Publish button on the Wordpress UI).
It's certainly returning the post id during an auto-save (as per the first block of code as you enter custom_add_save), though.
Maybe you'd like to look into that. :)
I developed a plugin with Settings link that was working fine in WordPress 2.7. Version 2.8 brings some additional security features that cause Settings link displaying message: You do not have sufficient permissions to access this page.
This is the API hook I use to create link:
function folksr_plugin_action($links, $file) {
if (strstr($file, 'folksr/folksr.php')) {
$fl = "Settings";
return array_merge(array($fl), $links);
}
return $links;
}
add_filter('plugin_action_links', 'folksr_plugin_action', 10, 2);
Full source code available at plugin page.
Settings screen does not contain any additional logic, just a couple of options and HTML echoed to the screen.
Suprisingly enough, Codex does not return anything for search phrase "plugin_action_links". Can you provide example or point me to working code for Settings link in Plugins menu?
I found the solution to my own problem by analyzing sources of some random plugins. I must say - what an unpleasurable experience that was! But hey, here's the solution.
It turns out that in order to build Settings link, it needs to be registered first. The following code is a stub that does the trick:
class MyPlugin {
public function __construct() {
add_filter('plugin_action_links', array($this, 'renderPluginMenu'), 10, 2);
add_action('admin_menu', array($this, 'setupConfigScreen'));
}
public function renderPluginMenu() {
$thisFile = basename(__FILE__);
if (basename($file) == $thisFile) {
$l = 'Settings';
array_unshift($links, $l);
}
return $links;
}
public function setupConfigScreen() {
if (function_exists('add_options_page')) {
add_options_page('MyPlugin settings', 'MyPlugin', 8, basename(__FILE__), array($this, 'renderConfigScreen'));
}
}
public function renderConfigScreen() {
include dirname(__FILE__) . '/MyPluginSettings.php';
}
}
I have working admin menu links on my plugins working in 2.8+. My function looks like this:
function plugin_action_links( $links, $file ) {
if ( $file == plugin_basename(__FILE__) )
$links[] = 'Settings';
return $links;
}
My add_filter line is mostly identical. I think the first thing to try is adding the use of the admin_url function.
Hope that helps.