Solved

PHP OOP Connect function to another

Posted on 2014-02-22
4
464 Views
Last Modified: 2014-02-23
I'm not sure how to ask this question and I don't have a real world example or well written code to show you, but I will do my best.
I have a Wordpress plugin that has a Functions.php file.
Function myContact() within the functions.php file creates a table.  
Each of the tables has a specific ID.
Each <tr> within the table has a specific ID.
Like so:
function myContact() {
echo "<table id='table_one'>
<tr id='field_one'>
<td>FIRST NAME</td><td>LAST NAME</>
</tr>
<tr id='field_two'>
<td>COMPANY NAME</td><td>PHONE NUMBER</>
</tr>

</table>";
}

Open in new window


I don't want to mess with the plugin core functions, cause if there is an update, I will loose the work. As you know.
So, I want to learn how to attach a custom function to another function. I want to attach my function to the table in the function above.  Like so:
My Custom Function.PHP file
function myCustomFunction() {
echo '<tr id='field_three'>
<td>EMAIL</td><td>CELL PHONE</>
</tr>
<tr id='field_four'>
<td>BIRTHDAY</td><td>NICK NAME</>
</tr>
}
add_action( 'myCustomFunction', myContact' ); INSERT AFTER tr id='field_two'

Open in new window


I want to insert my function after a specific <tr id> in the original function.
Does that make sense???
0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39879707
I don't think that is going to work. Usually the function you are overriding should be written to support overridding - something like
function myContact() {
echo "<table id='table_one'>
<tr id='field_one'>
<td>FIRST NAME</td><td>LAST NAME</>
</tr>
<tr id='field_two'>
<td>COMPANY NAME</td><td>PHONE NUMBER</>
</tr>";
 if (function_exists('customMyContact')) {
   customMyContact();
 }
echo "
</table>";
}

Open in new window

                                 
You can now extend the function by defining your customMyContact function.

However because the pluggin generates the complete table in one function with no external calls your only option is to replace the function with your own, modify the function to call your own or use javascript to modify the table at load time.

Either of the first two options fall foul of the pluggin upgrade overwrite problem.

The javascript option will work - basically you add some javascript code that appends the required information to the table. With JQuery it would be something like this

<script type="text/javascript">
// POPULATE YOUR HTML STRING HERE WITH PHP CODE INSERTS
var html="tr id='field_three'><td>EMAIL</td><td><?php echo $cell_phone;?></></tr><tr id='field_four'><td><?php echo $dob;?></td><td><?php echo $nickname;?></></tr>";
$(function() {
  $('#table_one').append(html);
});
</script>

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 39879721
OK, another question, what does the "10' and '2' signify at the end?
add_action('personal_details_list', 'custom_purchase_details', 10, 2);
0
 
LVL 57

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 39880631
The 10 paramter refers to the priority given to the action - the lower the number the sooner it executes. The default is 10

The last parameter refers to the number of parameters the hook accepts.

You can read more about the add_action call here
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Knowledge base software has turned out to be a quite reliable method for storing information, promoting collaborative work and for sharing valuable input and solutions.However, some organizations are trying to develop a knowledge base that works wit…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The viewer will learn how to count occurrences of each item in an array.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question