Im trying to change the_title() for my wordpress website page, however it changes all the text and I only what to change 'ORDER' to 'QUOTE' and leave the order number '#3344'. Could anyone help me with my if statement.
<?php
// Changeing Order title to quote
$page_id = get_the_ID();
if($page_id == '49')
{
$original_title = get_the_title();
$new_title = str_replace("ORDER","QUOTE","ORDER");
echo $new_title;
}
else {
the_title();
}
?>
Please have a close look at the docs. However - you have to use the wp_title() function:
Example code:
wp_title( string $sep = '»', bool $display = true, string $seplocation = '' )
Parameters:
$sep (string) (Optional) default is '»'. How to separate the various items within the page title.
$display (bool) (Optional) Whether to display or retrieve title.
(string) (Optional) Direction to display title, 'right'.
I'm pretty sure wp_title will do the job right :)
Some more information you can find here: !! hit me !!
Related
I'm making a custom plugin for a WordPress powered page to disable a certain button on condition.
Context: To disable place order button if the user breached a certain credit limit.
I'm expecting that upon entering checkout page, the plugin will fire and check if the user exceeded the credit limit. If yes, it will then alert the user and disable the place order button.(OR create an overlay OR any other methods)
For now I have this:
function credit_limit_check()
{
/** page 13 is the check out page ID*/
if (is_page(13)) {
if (is_user_logged_in()) {
/* For reference purpose*/
/* get_user_meta( int $user_id, string $key = '', bool $single = false ) */
$ID = get_current_user_id();
$credit_val = get_user_meta($ID, 'credit_limit', true);
$outstanding_val = get_user_meta($ID, 'outstanding_credit', true);
$credit_breach = $credit_val - $outstanding_val;
if ($credit_breach <= 0) {
/*disable checkout button if not enough credit limit*/
echo '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="Place order" data-value="Place order" disabled>Place order</button>';
echo '<script>alert("You have exceeded your credit limit, please make sure you have no outstanding bill")</script>';
} else {
print_r("Huzzah! Good news! You have enough credit to proceed!");
}
} else {
print_r("Please login with your account before proceeding.");
}
}
}
The only problem is that the functions actually creates an extra button on top of the page instead of disabling the original button. So I am wondering if this is actually doable, or do I have to modify the html files directly to achieve what is intended.(Preferably not)
Now, I do see some similar questions, but all of them require directly applying php in the html tags. It is not applicable for my situation as I am creating a wordpress custom plugin. (Which is an individual php file).
Well I would solve this in two parts.
Step 1 . Adding proper notices.
function add_notices_for_checkout_credit()
{
if(is_checkout()) {
if (is_user_logged_in()) {
$ID = get_current_user_id();
$credit_breach = getUserCredit($ID);
if ($credit_breach <= 0) {
wc_add_notice( 'You have exceeded your credit limit, please make sure you have no outstanding bill', 'error' );
} else {
wc_add_notice( 'Huzzah! Good news! You have enough credit to proceed!', 'success' );
}
}
}
}
add_action( 'wp', 'add_notices_for_checkout_credit');
function getUserCredit($ID) {
$credit_val = get_user_meta($ID, 'credit_limit', true);
$outstanding_val = get_user_meta($ID, 'outstanding_credit', true);
$credit_breach = $credit_val - $outstanding_val;
return $credit_breach;
}
With less credit it shows.
When sufficient credit it shows.
Step 2 . Restricting button
function change_button($order_button)
{
if (is_user_logged_in()) {
$ID = get_current_user_id();
$credit_breach = getUserCredit($ID);
if ($credit_breach <= 0) {
$order_button_text = __( "No Credit", "woocommerce" );
$style = ' style="color:#fff;cursor:not-allowed;background-color:#999;text-align:center"';
return '<a class="button alt"'.$style.' name="woocommerce_checkout_place_order" id="place_order" >' . esc_html( $order_button_text ) . '</a>';
}
}
return $order_button;
}
add_filter( 'woocommerce_order_button_html', 'change_button');
This will disable the button, and you should be good to go.
Don't check for guest in your code, change it from your woocommerce setting. In Account setting mark it compuslory to login when checking out.
PS : For extra security use woocommerce_checkout_update_order_review to check if its a valid request because someone can create a submit button using developer tool.
I have a textarea where I can enter the content:
<textarea name="content" placeholder="Content"></textarea>
The type of the content column in the DB is Text.
So I could add text there and then insert that text to the DB:
$stmt = $conn->prepare('INSERT INTO content VALUES(?)');
$stmt->execute( [$content] );
Then I show that content some where on my website:
$stmt = $conn->prepare('SELECT content FROM posts');
$stmt->execute();
$results = $stmt->fetchAll();
foreach( $results as $result ){
echo '<div>'. $result .'</div>';
}
But that content is then showed as a plain text, So if I entered:
$content = "This content contains a URL http://example.com";
I get: This content contains a URL http://example.com, So the link is not shown as a link, But a plain text.
Also if I added an image:
$content = "http://example.com/images/img.jpg";
Or a video:
$content = "http://example.com/images/video.mp4";
Or a video from Youtube.
So what should I do?
Should I use PHP or Javascript to check if the content contains a URL/image/video, Then add the related html elements to that URL?
I would not recommend using an editor like CKEditor just to wrap some URLs in markup, as others have shockingly suggested. That is a very lazy and expensive (not necessarily price but size of files and number of requests) way of solving a simple task.
The following solution is untested and the regex patterns were taken from external sources, so I unfortunately can't guarantee their correctness. Try it yourself and test, test, test.
EXAMPLE
// your string
$content = "This is the content https://example.com/images/image1.jpg";
// find all URLs in $content and add matches to $matches array
$regex = "#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#";
preg_match_all($regex, $content, $matches);
// loop through $matches array
foreach ($matches as $match) {
// check each item in array and use regex to determine type
if (preg_match('/\.(jpg|jpeg|png|gif)(?:[\?\#].*)?$/i', $match)) {
$markup = '<img src="'.$match.'">';
} else {
$markup = ''.$match.'';
}
// now replace the $match'ed URL in $content with the right $markup
str_replace($match, $markup, $content);
}
DOCS
preg_match_all: http://php.net/manual/en/function.preg-match-all.php
preg_match: http://php.net/manual/en/function.preg-match.php
str_replace: http://php.net/manual/en/function.str-replace.php
I have a feature, where users can post thoughts and users can add comments to each thought The way it works is that, when the comments link is clicked, all comments associated with that thought_id will be loaded.
Here is the structure of my user_comments table:
id
body_of_msg
comment_posted_by
comment_posted_to
post_id (which is the id of a thought from the `user_thoughts` table)
Consider the following data:
user_thoughts table (1 row):
id: 184
thought: "hello, this is a thought from anderson."
Assume I have two rows in the user_comments table:
id: 1
body_of_msg: Comment assigned to thought_id of 184
comment_posted_by: conor
comment_posted_to: anderson
post_id: 184
id: 2
body_of_msg: Another comment assigned to thought_id of 184
comment_posted_by: alice
comment_posted_to: anderson
post_id: 184
Problem: At the moment, when I click the comments link, only one of the comments is being shown (the latest comment is being shown, so in this case, Alice's comment).
Here is the code:
<?php
// Get the comments attached to a users post...
$get_comm = mysqli_query ($connect, "SELECT * FROM user_comments WHERE post_id='$thought_id' ORDER BY post_id DESC");
$num_of_comments = mysqli_num_rows($get_comm); // get number of comments for each post by post_id
// if there are comments for the post, get its content
if ($num_of_comments !=0 || $num_of_comments == 0){
while( $comment = mysqli_fetch_assoc ($get_comm)){
$comment_body = $comment['body_of_msg'];
$comment_posted_to = $comment['comment_posted_to'];
$comment_posted_by = $comment['comment_posted_by'];
$removed = $comment['comment_removed'];
}
echo "";
/** There are other divs and content echo'd here**/
////////////////////////////////////////////
// this is where each comment is displayed
echo "
<div id='toggleComment$thought_id' class='new_comment' style='display:none;'>
<br/><b><a href = 'profile_page/$comment_posted_by'> $comment_posted_by said</a></b>: $comment_body "; ?><?php
if ($comment_posted_by == $username){
echo "<a id='remove_comment' href = '/inc/remove_comment.php'> Delete </a>";
} echo "
</div>";
/////////////////////////////////////////////
}
?>
Where $thought_id comes from:
$count = mysqli_query ($connect, "SELECT * FROM user_thoughts");
while ($row = mysqli_fetch_assoc($get_thoughts_from_db)) {
$thought_id = $row['id'];
}
What I think:
This could just be me struggling to find a solution, but, could it be that each comment is overlapping the other? My comment feature involved comments dynamically appearing below the thought, so I have utilized Javascript, to achieve this. Just thinking the block may be getting replaced by a new comment?
What I have tried:
while( $comment = mysqli_fetch_assoc ($get_comm)){
$comment_body = $comment['body_of_msg'];
$comment_posted_to = $comment['comment_posted_to'];
$comment_posted_by = $comment['comment_posted_by'];
$removed = $comment['comment_removed'];
// this is where each comment is displayed
echo "
<div id='toggleComment$thought_id' class='new_comment' style='display:none;'>
<br/><b><a href = 'profile_page/$comment_posted_by'> $comment_posted_by said</a></b>: $comment_body "; ?><?php
if ($comment_posted_by == $username){
echo "<a id='remove_comment' href = '/inc/remove_comment.php'> Delete </a>";
} echo "
</div>";
}
}
This still only shows one comment, when there are two to be shown.
Why don't you use ajax? I had done a site web that use comments (like stackoverflow) and I used a lot ajax for that. Of course, all the creations of html elements will be done in js and what you will return from php will be only json (containing the content of comments and its info).
This will help you also to load new comments without refreshing the page (setInterval).
For the answer, I have find three things that are strange, the first one is
if ($num_of_comments !=0 || $num_of_comments == 0){
which will be always true. The second thing is the fact that the echo is outside the while bloc (this probably the cause of having one comment echoed). The last thing is display none that you put in the style of the html element. So what I suggest you is to make the echo in the while block or to make an array of comments and make an iterator. After that, try to use the inspector tool of your browser to see if the code source returned contain only one comment or more. This will help you to see if the php works or not.
I'm trying to get a quantity value from external link, but I can't see this value until particular colour or size is selected (selection on that website works using JavaScript void(0) ).
Is it possible to trigger a link somehow and get the value after? Any suggestions?
However I know how to get a static value from url, see below:
$url = 'http://www.website.com/page.html';
$content = file_get_contents($url);
$first_step = explode( '<span id="quantity">' , $content );
$second_step = explode("</span>" , $first_step[1] );
echo $second_step[0];
Maybe solution, you can to split the process into two parts :
get all elements with regexp
preg_match_all('/<span [^>]+>/i',$content , $match);
print_r($match);
search attrs array of the result
$spans = array();
foreach( $match as $tag)
{
preg_match_all('/(id)=("[^"]*")/i',$tag, $spans[$tag]);
}
print_r($spans);
I am using the following to encode the html source of a ckeditor in a web application.
var updateString = app.getValue('wysiwygHomePage');
var encodedString = encodeURIComponent(updateString);
alert(encodedString);
app.httpRequest("www.xxxx.com/techy/savealldata.php", "GET", function(data, error, httpResponse){
alert(data);
},
{
"updateType":"homePage","updateString":encodedString}, "String", {}, {});
}
Then at the PHP end I am using :
<?php
$updateType = $_GET["updateType"];
$updateString = $_GET["updateString"];
$updateString2 = urldecode($updateString);
echo 'success here '.$updateType .' '.$updateString2 ;
?>
I am adding some coloured tex and the html source for this is:
<p>
<span style="color: rgb(255, 140, 0);">123</span><br />
</p>
<p>
This works okay until I cut and paste more than 32 times.
I then just get error returned from the PHP call.
I presume there are to many chars arriving at the PHP end ???
Any ideas why this is happening ?
Mr WARBY.
UPDATED PHP Code.
<?php
include 'dbdata.php';
$updateType = $_POST["updateType"];
$updateString = $_POST["updateString"];
$updateString2 = urldecode($updateString);
//echo 'success here '.$updateType .' '.$updateString2 ;
if($updateType === 'homePage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 12";
//echo $query5;
echo 'Home Page Updated 2';
mysql_query($query5);
}
if($updateType === 'instructionPage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 13";
echo 'Instruction Page Updated 2';
mysql_query($query5);
}
if($updateType === 'FAQPage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 14";
echo 'FAQ Page Updated';
mysql_query($query5);
}
?>
There are a lot of variables in play here. You need to change your debugging strategy. Instead of testing end to end each time try isolating each component.
In Javascript, call "app.getValue('wysiwygHomePage')", encode the string, decode the string, and put it right back in the editor. Do that in a loop until you can determine if the client-side is mangling anything.
If not, try encoding a complicated string in Javascript, sending it to a PHP script that decodes/re-encodes and echos it back. Do that in a loop several times.
If you still haven't found the problem try making a PHP script that takes a complicated string, INSERTS it, SELECTs it, UPDATEs it in a loop to see if you database encoding or escaping is affecting it.
If at any point you find the string changing when it shouldn't you've probably found your problem.